Modeling forecast count for MH

Log

Version 2023-03-29.
Code was initially made in SAS and send by Jemisha Apajee. File Name: “Analysis_common_diseases_step_by_step_20221123.sas”

Version 2023-04-19
Translated to R by Javier Silva-Valencia, adapted to Mental Health paper

Version 2023-05-17
Whitout social problems

Version 2023-06-07
Code debugged by Percy Soto-Becerra. The model’s problem was fixed. Diagnostic regression assessment was performed.

Context/Notes

  • This is part of a mental health paper, where we are analyzing the amount of MH-related visits among several countries (countries part of INTREPID) in a pre and during pandemic period.

  • The main variables are: Amount of total visits, amount of visits related to MH, 7-9 categories of MH groups, Tipe of visit(in-person, virtual) all this by month from 2018 to 2021 (Except Perú that has data from 2019-2021)

  • This specific code is for creating a model that fits the time series and creating a forecast to compare prepandemic and pandemic trend.

Getting ready

Load packages

Code
if (!require("pacman")) install.packages("pacman")

pacman::p_load(rio, 
               here,
               tidyverse,
               purrr, 
               patchwork, 
               scales, 
               DT, 
               janitor, 
               lubridate, 
               gtsummary, 
               broom, 
               broom.mixed,
               ggeffects, 
               lme4, 
               nlme, 
               forecast, 
               MASS, 
               mgcv, 
               tsModel,
               zoo, 
               DHARMa, 
               patchwork,
               tsibble, 
               feasts, 
               qqplotr, 
               parameters, 
               flextable,
               stringr, 
               imputeTS)

source(here("source", "diag_glmm.R"))
source(here("source", "sti_plotter_pre.R"))
source(here("source", "sti_model.R"))
source(here("source", "sti_model_china.R"))
source(here("source", "sti_model_norway.R"))
source(here("source", "sti_model_usa.R"))
source(here("source", "sti_effect_ploter.R"))

# rm(list = ls())       #To clear the environment and start from zero.
# 
# Colors' palette
cbPalette <- c("#999999",  # 1
               "#E69F00",  # 2
               "#56B4E9",  # 3
               "#009E73",  # 4
               "#F0E442",  # 5
               "#0072B2",  # 6
               "#D55E00",  # 7
               "#CC79A7")  # 8 

Read in data

Code
mh_visits <- import(here("Data", "All_countries_MHcounts.xlsx"), sheet = "MH_counts")

View data structure

Code
datatable(head(mh_visits, 100))    #Seeing first 10 rows of the dataset

Cleaning

Creating variables

Create date:

Code
mh_visits <- mh_visits %>%
  mutate(date = as.Date(paste(month, "01", year), format = "%m %d %Y")) %>% 
  arrange(country, date)

Variables of interest for Mental Healht (MH) analysis

Code
# Select variable of interest
mh_visits1 <- mh_visits |> 
  dplyr::select(country, 
         mh_category,
         year,
         month,
         date,
         den_total, 
         total_counts, 
         in_person_counts, 
         virtual_counts, 
         period)

#Sorting
mh_visits1 <- mh_visits1 %>% 
  arrange(country, date, mh_category)

# Rename columns
mh_visits1 <- mh_visits1 %>%
  rename(numerator = total_counts,         #MH counts
         denominator = den_total)          #Total visits per month

Check for missings

Code
mh_visits1 %>%
  tabyl(country, period, show_na = TRUE)           #No missings
   country pandemic1 pandemic2 pre-pandemic washout
 Argentina        99        90          234       9
 Australia        99        90          234       9
    Canada        99        90          234       9
     China       108        99          225       0
    Norway        99        90          234       9
      Peru        99        90          126       9
 Singapore        99        90          234       9
    Sweden        99        90          234       9
       USA        99        90          234       9

Sort categories of MH

Code
#Eliminando categorias eliminadas
mh_visits2 <- mh_visits1 %>% 
  filter(!(mh_category %in% c("Social Problems"))) 

# Establecer el nuevo orden para las categorías
new_order <- c("Anxiety and Mood Disorders", 
               "Bipolar, Schizophrenia and other Psycotic Disorders",
               "Dementia", 
               "ADHD and Eating Disorders", 
               "Sleep Disorders", 
               "Substance-Related and Addictive Disorders")

# Reorder categories
mh_visits2 <- mh_visits2 %>% 
  filter(mh_category %in% new_order) %>% 
  mutate(mh_category = fct_relevel(factor(mh_category), new_order))

Set up data for modelling

Code
pandemic_start       <- as.Date("2020-04-01")        #For all the countries the pandemic start april 2020 *To revise

pandemic_start_china <- as.Date("2020-02-01")  #For china pandemic start at feb 2020

mh_visits5           <- mh_visits2 %>%
  # create t = 1, ..., 48 for each country (except peru)
  mutate(
    tn = if_else(country != "Peru", 
                 (12 * (year - 2018)) + month, 
                 (12 * (year - 2019)) + month),
  time = tn, # for random effects in the residuals
  #Spliting N# visits into two variables (pre and post start of pandemic)   
  y_count = case_when(
    country != "China" & date < pandemic_start ~ numerator,
    country == "China" & date < pandemic_start_china ~ numerator,
    TRUE ~ NA_real_
    ),
  observed_count = case_when(
    country != "China" & date >= pandemic_start ~ numerator,
    country == "China" & date >= pandemic_start_china ~ numerator,
    TRUE ~ NA_real_
    ),
      
  # Dummy variables for China (changes in their health system administration)
  China_Jan19 = if_else(date >= as.Date("2019-01-01"), 1, 0),
  China_Dec20 = if_else(date >= as.Date("2020-12-01"), 1, 0),
  Norway_Feb21 = if_else(time >= 38, 1, 0), 
  USA_systdown = if_else(time >= 37 & time <= 39, 1, 0), 
  
  # Pre-post tag variable
  country = as.factor(country),
  pre_post = if_else(period == "pre-pandemic", 0, 1), 
  pre_postTo = if_else(is.na(y_count), "post", "pre"),      #pre/post binary variable
      
  #"tn" as a factor (will be a classification variable)
  tn = as.factor(tn), 
  
  #Creating variable for interaction pre-post exposure (wahsout would be on pre exposure)
  To = ifelse(period %in% c("pre-pandemic", "washout"), 0, 1), 
  
  # Creating rate variable for graphics
  rate = 10000 * numerator / denominator
  ) 

mh_visits_all <- mh_visits5 %>% 
  group_by(country, date) %>% 
  summarize(
    year = max(year),
    month = max(month),
    denominator = max(denominator),
    numerator = sum(numerator),
    observed_count = sum(observed_count),
    To = max(To), 
    time = max(time),
    period = max(period), 
    China_Jan19 = max(China_Jan19), 
    China_Dec20 = max(China_Dec20)
  ) %>%
  ungroup() |> 
  mutate(mh_category = "All common Mental Health disorders", 
         Norway_Feb21 = if_else(time >= 38, 1, 0), 
         USA_systdown = if_else(time >= 37 & time <= 39, 1, 0))

mh_visits_all  |> 
  bind_rows(mh_visits5) -> mh_visits5b

Pre-pandemic behaviour of outcomes (blinded to post-pandemic)

Code
min_tdate <- min(mh_visits5$date) #- month(1)
max_tdate <- max(mh_visits5$date) #+ month(1)

countryc <-  mh_visits5 |> 
  count(country) |> 
  pull(country) |> 
  as.character()

mh_categoryc <- mh_visits5 |> 
  count(mh_category) |> 
  pull(mh_category) |> 
  as.character()
Code
combinations <- expand.grid(country = countryc, mh_category = mh_categoryc)

combinations <- combinations %>%
  mutate(plot = map2(country, mh_category, ~ 
                       sti_plotter_pre(.x, .y, 
                                       blind = FALSE, 
                                       data = mh_visits5))) %>%
  pwalk(~ ggsave(filename = paste0(..1, "_", ..2, ".png"), 
                 plot = ..3, 
                 device = "png", 
                 path = here("img", "exploratory_plots"), 
                 scale = 2, 
                 width = 7, 
                 height = 7, 
                 units = "cm", 
                 dpi = 900
                 ))

# Extrae la lista de gráficos
plots_list <- combinations$plot
Code
i <- 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Code
i <- i + 1
plots_list[[i]]

Creating and Testing the Model

Code
# Countries and categories
countryc <-  mh_visits5b |> 
  count(country) |> 
  pull(country) |> 
  as.character()

countryc <- rep(countryc, 7)


mh_categoryc <- mh_visits5b |> 
  mutate(
    mh_category = 
      factor(mh_category, 
             levels = c("All common Mental Health disorders", 
                        "Anxiety and Mood Disorders", 
                        "ADHD and Eating Disorders", 
                        "Bipolar, Schizophrenia and other Psycotic Disorders", 
                        "Dementia", 
                        "Sleep Disorders", 
                        "Substance-Related and Addictive Disorders"))) |> 
  count(mh_category) |> 
  pull(mh_category) |> 
  as.character()

Modelling

Set global options

Code
# set options of graphics
alfa0 <- 0.5
alfa_ribbon0 <- 0.25
color_point0 <- c("#99B933")
color_line0 <- c("#002D72")
color_interrupt0 <- c("red")
color_ribbon_ex0 <- c("skyblue")
color_ribbon_def0 <- c("orange")
mindate0 <- "2018-01-01"
maxdate0 <- "2022-01-01"

Outcome: All common Mental Health disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
i <- 0
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.03
AIC=140.06   AICc=140.15   BIC=141.93
Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.03
AIC=140.06   AICc=140.15   BIC=141.93
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.932079e-07 (Intr)
time        5.906248e-09 0     
Residual    2.683836e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.16082929  0.08349518  0.10144566 -0.08609451 -0.32118000 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -3.452477 0.02188146 40 -157.78091  0.0000
time                    -0.004964 0.00137534 40   -3.60898  0.0008
To                       0.109672 0.03503510 40    3.13033  0.0033
harmonic(month, 2, 12)1 -0.033729 0.01991114 40   -1.69400  0.0980
harmonic(month, 2, 12)2  0.042649 0.00824037 40    5.17556  0.0000
harmonic(month, 2, 12)3  0.027202 0.01976296 40    1.37644  0.1763
harmonic(month, 2, 12)4  0.033617 0.00829591 40    4.05222  0.0002
To:I(time - tpand)       0.001727 0.00249365 40    0.69266  0.4925
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.903                                            
To                       0.417 -0.613                                     
harmonic(month, 2, 12)1 -0.101  0.110 -0.090                              
harmonic(month, 2, 12)2 -0.005 -0.026  0.084  0.010                       
harmonic(month, 2, 12)3  0.104 -0.108  0.136 -0.007     0.029             
harmonic(month, 2, 12)4 -0.009  0.026 -0.029 -0.093    -0.030     0.011   
To:I(time - tpand)       0.443 -0.468 -0.293  0.041    -0.020    -0.027   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.038   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.18868406 -0.73664106  0.05067041  0.61414783  2.21978444 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab0 |> 
  knitr::kable()
country mh_category effect estimate
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.116:  log likelihood = -70.75
AIC=143.5   AICc=143.59   BIC=145.37
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.946789e-07 (Intr)
time        5.680489e-09 0     
Residual    3.696840e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.05932616 -0.30738443  0.09328414 -0.25808594 -0.28097780 -0.44210280 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -3.330240 0.011270987 40 -295.47011  0.0000
time                    -0.000602 0.000694956 40   -0.86688  0.3912
To                       0.115781 0.016118523 40    7.18309  0.0000
harmonic(month, 2, 12)1 -0.033861 0.014614855 40   -2.31688  0.0257
harmonic(month, 2, 12)2  0.046119 0.006630108 40    6.95593  0.0000
harmonic(month, 2, 12)3  0.033354 0.015443595 40    2.15975  0.0368
harmonic(month, 2, 12)4 -0.030540 0.006656039 40   -4.58825  0.0000
To:I(time - tpand)      -0.003572 0.001088861 40   -3.28051  0.0022
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.917                                            
To                       0.498 -0.699                                     
harmonic(month, 2, 12)1 -0.024  0.043 -0.078                              
harmonic(month, 2, 12)2 -0.029  0.003  0.046 -0.041                       
harmonic(month, 2, 12)3  0.178 -0.150  0.094  0.014     0.038             
harmonic(month, 2, 12)4 -0.004  0.022 -0.018  0.001    -0.005    -0.025   
To:I(time - tpand)       0.488 -0.487 -0.170  0.083    -0.015     0.111   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.3357351 -0.5897083  0.1865211  0.8621850  1.6078218 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.5420
s.e.  0.1117

sigma^2 = 1.038:  log likelihood = -68.67
AIC=141.34   AICc=141.61   BIC=145.09
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.788071e-07 (Intr)
time        8.196031e-09 0     
Residual    4.052217e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.72282181 -0.41456721  0.16147478 -0.13912600  0.04050206 -0.10468933 
       Phi7        Phi8        Phi9       Phi10       Phi11 
-0.05130863  0.05399622  0.03042363 -0.02024095 -0.31233934 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.3530457 0.020081536 40 -117.17459  0.0000
time                     0.0055529 0.001257661 40    4.41529  0.0001
To                       0.1943678 0.030431593 40    6.38704  0.0000
harmonic(month, 2, 12)1  0.0474961 0.010296480 40    4.61284  0.0000
harmonic(month, 2, 12)2 -0.0170364 0.007081764 40   -2.40567  0.0209
harmonic(month, 2, 12)3 -0.0081269 0.010546546 40   -0.77057  0.4455
harmonic(month, 2, 12)4 -0.0131689 0.007003002 40   -1.88046  0.0673
To:I(time - tpand)      -0.0061013 0.002182289 40   -2.79585  0.0079
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.924                                            
To                       0.468 -0.662                                     
harmonic(month, 2, 12)1 -0.131  0.095 -0.070                              
harmonic(month, 2, 12)2  0.016 -0.052  0.134 -0.015                       
harmonic(month, 2, 12)3  0.096 -0.148  0.323 -0.019     0.026             
harmonic(month, 2, 12)4 -0.068  0.088 -0.075 -0.018    -0.049    -0.046   
To:I(time - tpand)       0.474 -0.464 -0.263  0.059    -0.050    -0.205   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.059   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1808627 -0.5387176 -0.2422736  0.5147350  3.2885588 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,1) with zero mean 

Coefficients:
          ar1      ar2     ma1
      -1.2467  -0.5986  0.9369
s.e.   0.1459   0.1300  0.2059

sigma^2 = 0.7631:  log likelihood = -44.29
AIC=96.58   AICc=97.92   BIC=102.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.605346e-08 (Intr)
time        5.775762e-13 0     
Residual    3.199887e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-1.2403656 -0.7000945  0.9999927 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -3.424175 0.06412162 26 -53.40125  0.0000
time                    -0.014036 0.00850394 26  -1.65048  0.1109
To                       0.682782 0.12528452 26   5.44985  0.0000
harmonic(month, 2, 12)1 -0.056323 0.03900108 26  -1.44414  0.1606
harmonic(month, 2, 12)2 -0.064135 0.02445878 26  -2.62215  0.0144
harmonic(month, 2, 12)3  0.017342 0.02454482 26   0.70653  0.4861
harmonic(month, 2, 12)4  0.017445 0.02518959 26   0.69254  0.4947
China_Jan19              1.415761 0.11237128 26  12.59896  0.0000
To:I(time - tpand)      -0.065020 0.01080001 26  -6.02038  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.852                                            
To                       0.758 -0.888                                     
harmonic(month, 2, 12)1 -0.687  0.806 -0.833                              
harmonic(month, 2, 12)2 -0.377  0.449 -0.423  0.441                       
harmonic(month, 2, 12)3  0.487 -0.560  0.615 -0.486    -0.245             
harmonic(month, 2, 12)4  0.378 -0.436  0.425 -0.365    -0.202     0.330   
China_Jan19              0.644 -0.940  0.810 -0.763    -0.431     0.511   
To:I(time - tpand)      -0.148  0.174 -0.541  0.472     0.197    -0.273   
                        h(,2,12)4 Ch_J19
time                                    
To                                      
harmonic(month, 2, 12)1                 
harmonic(month, 2, 12)2                 
harmonic(month, 2, 12)3                 
harmonic(month, 2, 12)4                 
China_Jan19              0.395          
To:I(time - tpand)      -0.092    -0.169

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1508487 -0.5402548 -0.1990516  0.4408839  2.5576068 

Number of Observations: 35
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Notes
  • Report in text (maybe in supplementary table?) that including period of shock (t > Dec 2020) does not significantly change the results.
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,2) with zero mean 

Coefficients:
          ma1      ma2
      -0.3084  -0.3545
s.e.   0.1729   0.1955

sigma^2 = 1.014:  log likelihood = -51.99
AIC=109.98   AICc=110.7   BIC=114.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.269883e-07 (Intr)
time        1.086626e-08 0     
Residual    9.572249e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
-0.2330876 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.4821158 0.010919582 29 -227.30868  0.0000
time                     0.0020698 0.000677876 29    3.05334  0.0048
To                       0.0356471 0.023171878 29    1.53838  0.1348
harmonic(month, 2, 12)1  0.0052142 0.006558578 29    0.79501  0.4331
harmonic(month, 2, 12)2 -0.0198656 0.006720127 29   -2.95614  0.0061
harmonic(month, 2, 12)3  0.0380010 0.007626037 29    4.98306  0.0000
harmonic(month, 2, 12)4  0.0102372 0.006775275 29    1.51096  0.1416
To:I(time - tpand)      -0.0010624 0.003591873 29   -0.29578  0.7695
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.878                                            
To                       0.320 -0.521                                     
harmonic(month, 2, 12)1 -0.145  0.109 -0.101                              
harmonic(month, 2, 12)2  0.006 -0.018  0.137 -0.098                       
harmonic(month, 2, 12)3  0.133 -0.227  0.520 -0.076     0.032             
harmonic(month, 2, 12)4  0.037 -0.003  0.001 -0.078    -0.009    -0.008   
To:I(time - tpand)       0.058 -0.028 -0.720  0.173    -0.119    -0.510   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.084   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.1357803 -0.5755922  0.1105106  0.4757616  1.7442943 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway All common Mental Health disorders Level change 1.04 (0.99 to 1.08), p = 0.135
Norway All common Mental Health disorders Trend change 1 (0.99 to 1.01), p = 0.77
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.4596
s.e.  0.1719

sigma^2 = 0.9012:  log likelihood = -48.82
AIC=101.64   AICc=102.01   BIC=104.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.569990e-07 (Intr)
time        3.451858e-09 0     
Residual    1.817722e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.09008322 -0.14179720 -0.15816761 -0.61118340 -0.45445075 -0.16982838 
       Phi7        Phi8 
 0.17268850 -0.39894690 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -4.043470 0.013142243 28 -307.66972  0.0000
time                     0.025395 0.001418969 28   17.89681  0.0000
To                       0.749106 0.013426691 28   55.79228  0.0000
harmonic(month, 2, 12)1 -0.022995 0.008420245 28   -2.73088  0.0108
harmonic(month, 2, 12)2  0.005221 0.006982622 28    0.74769  0.4609
harmonic(month, 2, 12)3 -0.053847 0.009449809 28   -5.69821  0.0000
harmonic(month, 2, 12)4  0.018314 0.006980597 28    2.62357  0.0139
To:I(time - tpand)      -0.029406 0.001438343 28  -20.44432  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.962                                            
To                       0.710 -0.840                                     
harmonic(month, 2, 12)1  0.042 -0.007 -0.054                              
harmonic(month, 2, 12)2 -0.012 -0.015  0.044 -0.095                       
harmonic(month, 2, 12)3  0.349 -0.338  0.172  0.026     0.025             
harmonic(month, 2, 12)4 -0.099  0.089 -0.022  0.026    -0.034    -0.144   
To:I(time - tpand)       0.919 -0.935  0.631  0.052     0.011     0.415   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.133   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.14035315 -0.35776236  0.08254759  0.51470508  1.76779570 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru All common Mental Health disorders Level change 2.12 (2.06 to 2.17), p < 0.001
Peru All common Mental Health disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway All common Mental Health disorders Level change 1.04 (0.99 to 1.08), p = 0.135
Norway All common Mental Health disorders Trend change 1 (0.99 to 1.01), p = 0.77
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.038:  log likelihood = -69
AIC=140   AICc=140.09   BIC=141.87
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.195175e-07 (Intr)
time        6.054890e-09 0     
Residual    1.348695e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.04959712 -0.06154859 -0.06379853  0.17909585 -0.17813190 -0.09681921 
       Phi7        Phi8        Phi9       Phi10       Phi11 
 0.05416645 -0.06537127 -0.12159967 -0.08546415 -0.40868122 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.710014 0.02385837 40 -239.32962  0.0000
time                     0.008747 0.00155266 40    5.63367  0.0000
To                       0.206288 0.04258769 40    4.84385  0.0000
harmonic(month, 2, 12)1 -0.023229 0.01082195 40   -2.14647  0.0380
harmonic(month, 2, 12)2  0.007227 0.01100509 40    0.65673  0.5151
harmonic(month, 2, 12)3  0.011564 0.01153176 40    1.00281  0.3220
harmonic(month, 2, 12)4 -0.009629 0.01085228 40   -0.88732  0.3802
To:I(time - tpand)       0.010027 0.00244761 40    4.09663  0.0002
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.936                                            
To                       0.564 -0.739                                     
harmonic(month, 2, 12)1 -0.127  0.108 -0.079                              
harmonic(month, 2, 12)2 -0.020 -0.012  0.046  0.029                       
harmonic(month, 2, 12)3  0.177 -0.247  0.422 -0.069     0.058             
harmonic(month, 2, 12)4 -0.010  0.020  0.001 -0.073    -0.046    -0.017   
To:I(time - tpand)       0.350 -0.288 -0.365  0.059    -0.008    -0.288   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1750600 -0.7130761 -0.2588606  0.3983306  2.4580597 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore All common Mental Health disorders Level change 1.23 (1.14 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Peru All common Mental Health disorders Level change 2.12 (2.06 to 2.17), p < 0.001
Peru All common Mental Health disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway All common Mental Health disorders Level change 1.04 (0.99 to 1.08), p = 0.135
Norway All common Mental Health disorders Trend change 1 (0.99 to 1.01), p = 0.77
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: All common Mental Health disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.058:  log likelihood = -69.47
AIC=140.95   AICc=141.03   BIC=142.82
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.858397e-07 (Intr)
time        4.231727e-09 0     
Residual    2.657486e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.072690433  0.158082444  0.008351295  0.027148432  0.035154355 -0.627804579 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.2875658 0.014318108 40 -159.76732  0.0000
time                     0.0054323 0.000878043 40    6.18679  0.0000
To                       0.0706213 0.021353481 40    3.30725  0.0020
harmonic(month, 2, 12)1  0.0189794 0.018194760 40    1.04313  0.3032
harmonic(month, 2, 12)2 -0.0287076 0.004687890 40   -6.12379  0.0000
harmonic(month, 2, 12)3  0.0184154 0.018095526 40    1.01767  0.3149
harmonic(month, 2, 12)4  0.0076209 0.004578440 40    1.66453  0.1038
To:I(time - tpand)       0.0014019 0.001467138 40    0.95555  0.3450
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.436 -0.647                                     
harmonic(month, 2, 12)1 -0.120  0.086 -0.033                              
harmonic(month, 2, 12)2  0.042 -0.062  0.120 -0.081                       
harmonic(month, 2, 12)3 -0.059  0.003  0.114  0.036    -0.060             
harmonic(month, 2, 12)4  0.004  0.035 -0.055 -0.057     0.002    -0.028   
To:I(time - tpand)       0.488 -0.489 -0.239 -0.013    -0.001    -0.163   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.024   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-3.28170355 -0.61370874 -0.03938239  0.64695097  2.04930975 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden All common Mental Health disorders Level change 1.07 (1.03 to 1.12), p = 0.002
Sweden All common Mental Health disorders Trend change 1 (1 to 1), p = 0.345
Singapore All common Mental Health disorders Level change 1.23 (1.14 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Peru All common Mental Health disorders Level change 2.12 (2.06 to 2.17), p < 0.001
Peru All common Mental Health disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway All common Mental Health disorders Level change 1.04 (0.99 to 1.08), p = 0.135
Norway All common Mental Health disorders Trend change 1 (0.99 to 1.01), p = 0.77
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.051:  log likelihood = -69.31
AIC=140.63   AICc=140.71   BIC=142.5
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.990877e-07 (Intr)
time        8.526998e-09 0     
Residual    2.195266e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
0.02661089 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -1.7839168 0.017062011 40 -104.55490  0.0000
time                     0.0022179 0.001078498 40    2.05644  0.0463
To                      -0.0277751 0.026998490 40   -1.02877  0.3098
harmonic(month, 2, 12)1  0.0394802 0.009114229 40    4.33171  0.0001
harmonic(month, 2, 12)2  0.0018055 0.008850194 40    0.20401  0.8394
harmonic(month, 2, 12)3 -0.0162941 0.009276194 40   -1.75655  0.0866
harmonic(month, 2, 12)4 -0.0055444 0.008900138 40   -0.62296  0.5368
To:I(time - tpand)       0.0045977 0.001957279 40    2.34902  0.0238
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.868                                            
To                       0.356 -0.592                                     
harmonic(month, 2, 12)1 -0.143  0.100 -0.052                              
harmonic(month, 2, 12)2 -0.038  0.002  0.032  0.002                       
harmonic(month, 2, 12)3  0.099 -0.144  0.251 -0.032    -0.015             
harmonic(month, 2, 12)4  0.017  0.005 -0.023 -0.005    -0.018     0.001   
To:I(time - tpand)       0.449 -0.519 -0.243  0.038     0.016    -0.099   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.014   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1330418 -0.5504796  0.1434106  0.6240550  2.3335762 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA All common Mental Health disorders Level change 0.97 (0.93 to 1.02), p = 0.31
USA All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.024
Sweden All common Mental Health disorders Level change 1.07 (1.03 to 1.12), p = 0.002
Sweden All common Mental Health disorders Trend change 1 (1 to 1), p = 0.345
Singapore All common Mental Health disorders Level change 1.23 (1.14 to 1.33), p < 0.001
Singapore All common Mental Health disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Peru All common Mental Health disorders Level change 2.12 (2.06 to 2.17), p < 0.001
Peru All common Mental Health disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway All common Mental Health disorders Level change 1.04 (0.99 to 1.08), p = 0.135
Norway All common Mental Health disorders Trend change 1 (0.99 to 1.01), p = 0.77
China All common Mental Health disorders Level change 1.98 (1.59 to 2.47), p < 0.001
China All common Mental Health disorders Trend change 0.94 (0.92 to 0.96), p < 0.001
Canada All common Mental Health disorders Level change 1.21 (1.15 to 1.28), p < 0.001
Canada All common Mental Health disorders Trend change 0.99 (0.99 to 1), p = 0.008
Australia All common Mental Health disorders Level change 1.12 (1.09 to 1.16), p < 0.001
Australia All common Mental Health disorders Trend change 1 (0.99 to 1), p = 0.002
Argentina All common Mental Health disorders Level change 1.12 (1.05 to 1.19), p = 0.003
Argentina All common Mental Health disorders Trend change 1 (1 to 1.01), p = 0.493
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.12 (1.05 to 1.19), p = 0.003

Trend change

1 (1 to 1.01), p = 0.493

Australia

Level change

1.12 (1.09 to 1.16), p < 0.001

Trend change

1 (0.99 to 1), p = 0.002

Canada

Level change

1.21 (1.15 to 1.28), p < 0.001

Trend change

0.99 (0.99 to 1), p = 0.008

China

Level change

1.98 (1.59 to 2.47), p < 0.001

Trend change

0.94 (0.92 to 0.96), p < 0.001

Norway

Level change

1.04 (0.99 to 1.08), p = 0.135

Trend change

1 (0.99 to 1.01), p = 0.77

Peru

Level change

2.12 (2.06 to 2.17), p < 0.001

Trend change

0.97 (0.97 to 0.97), p < 0.001

Singapore

Level change

1.23 (1.14 to 1.33), p < 0.001

Trend change

1.01 (1.01 to 1.01), p < 0.001

Sweden

Level change

1.07 (1.03 to 1.12), p = 0.002

Trend change

1 (1 to 1), p = 0.345

USA

Level change

0.97 (0.93 to 1.02), p = 0.31

Trend change

1 (1 to 1.01), p = 0.024

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Anxiety and Mood Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.061:  log likelihood = -69.52
AIC=141.04   AICc=141.13   BIC=142.91
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.591735e-07 (Intr)
time        8.006211e-09 0     
Residual    2.545325e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.16138058  0.09500984  0.16674955 -0.08548189 -0.43256855 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -3.800860 0.02323842 40 -163.55930   0.000
time                    -0.008031 0.00147007 40   -5.46307   0.000
To                       0.207874 0.03702527 40    5.61438   0.000
harmonic(month, 2, 12)1 -0.028836 0.02545260 40   -1.13292   0.264
harmonic(month, 2, 12)2  0.041202 0.00790594 40    5.21147   0.000
harmonic(month, 2, 12)3  0.018212 0.02513936 40    0.72445   0.473
harmonic(month, 2, 12)4  0.028253 0.00795516 40    3.55154   0.001
To:I(time - tpand)       0.005824 0.00262111 40    2.22184   0.032
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.904                                            
To                       0.427 -0.625                                     
harmonic(month, 2, 12)1 -0.103  0.115 -0.092                              
harmonic(month, 2, 12)2 -0.003 -0.030  0.097  0.036                       
harmonic(month, 2, 12)3  0.087 -0.079  0.101  0.000     0.031             
harmonic(month, 2, 12)4 -0.008  0.027 -0.031 -0.111    -0.035     0.038   
To:I(time - tpand)       0.452 -0.475 -0.275  0.032    -0.026    -0.025   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.042   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.35755543 -0.66320592  0.03942071  0.65657986  2.32936383 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,1) with zero mean 

Coefficients:
          ar1     ma1
      -0.4397  0.8526
s.e.   0.1980  0.1227

sigma^2 = 0.9512:  log likelihood = -66.17
AIC=138.35   AICc=138.89   BIC=143.96
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.181207e-07 (Intr)
time        3.969713e-09 0     
Residual    3.365618e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
 0.1335535 -0.3340768  0.1096485 -0.2789437 -0.2507490 -0.4412085 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -3.612990 0.011792214 40 -306.38773  0.0000
time                    -0.000813 0.000726960 40   -1.11835  0.2701
To                       0.092133 0.016946200 40    5.43678  0.0000
harmonic(month, 2, 12)1 -0.039993 0.016003698 40   -2.49897  0.0167
harmonic(month, 2, 12)2  0.040256 0.007000316 40    5.75057  0.0000
harmonic(month, 2, 12)3  0.026395 0.016920312 40    1.55997  0.1266
harmonic(month, 2, 12)4 -0.030266 0.007026692 40   -4.30736  0.0001
To:I(time - tpand)      -0.004327 0.001152668 40   -3.75361  0.0006
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.917                                            
To                       0.496 -0.696                                     
harmonic(month, 2, 12)1 -0.018  0.041 -0.077                              
harmonic(month, 2, 12)2 -0.028  0.002  0.047 -0.038                       
harmonic(month, 2, 12)3  0.186 -0.154  0.091  0.017     0.040             
harmonic(month, 2, 12)4 -0.005  0.023 -0.018 -0.002    -0.006    -0.020   
To:I(time - tpand)       0.484 -0.482 -0.180  0.082    -0.016     0.119   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.039   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.1642999 -0.7017196  0.1560169  0.9089529  1.5773536 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.5932
s.e.  0.1179

sigma^2 = 0.9968:  log likelihood = -67.74
AIC=139.49   AICc=139.75   BIC=143.23
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.433539e-07 (Intr)
time        5.929259e-09 0     
Residual    3.564579e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.584123970 -0.374846652  0.113012997 -0.230500244  0.083003896 -0.183975516 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.127570443  0.049103025  0.002047399 -0.018200509 -0.315852719 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.6018930 0.016344447 40 -159.19125  0.0000
time                     0.0046973 0.001043251 40    4.50252  0.0001
To                       0.2492474 0.026154294 40    9.52988  0.0000
harmonic(month, 2, 12)1  0.0479674 0.011770720 40    4.07515  0.0002
harmonic(month, 2, 12)2 -0.0161088 0.007489049 40   -2.15099  0.0376
harmonic(month, 2, 12)3 -0.0048958 0.011788032 40   -0.41532  0.6801
harmonic(month, 2, 12)4 -0.0135749 0.007430823 40   -1.82684  0.0752
To:I(time - tpand)      -0.0066214 0.001665934 40   -3.97461  0.0003
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.929                                            
To                       0.539 -0.723                                     
harmonic(month, 2, 12)1 -0.169  0.143 -0.121                              
harmonic(month, 2, 12)2  0.000 -0.026  0.083 -0.011                       
harmonic(month, 2, 12)3  0.099 -0.136  0.259 -0.021     0.009             
harmonic(month, 2, 12)4 -0.039  0.051 -0.034 -0.020    -0.042    -0.027   
To:I(time - tpand)       0.416 -0.385 -0.273  0.058    -0.033    -0.176   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.057   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1121423 -0.6024484 -0.1166695  0.5957475  3.8567171 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
          ma1
      -0.5897
s.e.   0.1490

sigma^2 = 0.8998:  log likelihood = -47.52
AIC=99.04   AICc=99.42   BIC=102.16
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.587021e-07 (Intr)
time        4.811424e-09 0     
Residual    2.592730e+00       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-1.2355566 -0.7324928  0.9999953 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -4.030300 0.06324461 26 -63.72559  0.0000
time                    -0.012543 0.00823999 26  -1.52216  0.1400
To                       0.722661 0.12126404 26   5.95940  0.0000
harmonic(month, 2, 12)1 -0.073035 0.03807972 26  -1.91794  0.0662
harmonic(month, 2, 12)2 -0.063433 0.02386320 26  -2.65821  0.0133
harmonic(month, 2, 12)3 -0.005743 0.02397007 26  -0.23958  0.8125
harmonic(month, 2, 12)4  0.043377 0.02465771 26   1.75915  0.0903
China_Jan19              1.507236 0.10929861 26  13.79007  0.0000
To:I(time - tpand)      -0.071216 0.01049930 26  -6.78290  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.839                                            
To                       0.746 -0.888                                     
harmonic(month, 2, 12)1 -0.669  0.800 -0.833                              
harmonic(month, 2, 12)2 -0.364  0.442 -0.417  0.441                       
harmonic(month, 2, 12)3  0.483 -0.562  0.619 -0.487    -0.237             
harmonic(month, 2, 12)4  0.366 -0.432  0.424 -0.365    -0.197     0.341   
China_Jan19              0.612 -0.935  0.807 -0.754    -0.422     0.510   
To:I(time - tpand)      -0.139  0.170 -0.538  0.478     0.196    -0.272   
                        h(,2,12)4 Ch_J19
time                                    
To                                      
harmonic(month, 2, 12)1                 
harmonic(month, 2, 12)2                 
harmonic(month, 2, 12)3                 
harmonic(month, 2, 12)4                 
China_Jan19              0.389          
To:I(time - tpand)      -0.093    -0.165

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.3294387 -0.5757847 -0.1898814  0.4716617  2.6475729 

Number of Observations: 35
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
          ma1
      -0.5853
s.e.   0.1474

sigma^2 = 0.9753:  log likelihood = -51.74
AIC=107.48   AICc=107.83   BIC=110.7
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.574038e-07 (Intr)
time        7.872051e-09 0     
Residual    8.369774e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
-0.3144784 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.8358149 0.010488437 29 -270.37535  0.0000
time                     0.0020576 0.000651623 29    3.15767  0.0037
To                       0.0335496 0.022474274 29    1.49280  0.1463
harmonic(month, 2, 12)1  0.0167855 0.006303587 29    2.66284  0.0125
harmonic(month, 2, 12)2 -0.0278993 0.006553238 29   -4.25733  0.0002
harmonic(month, 2, 12)3  0.0413894 0.007367663 29    5.61772  0.0000
harmonic(month, 2, 12)4  0.0034675 0.006598555 29    0.52550  0.6032
To:I(time - tpand)      -0.0025813 0.003484793 29   -0.74075  0.4648
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.879                                            
To                       0.327 -0.527                                     
harmonic(month, 2, 12)1 -0.149  0.110 -0.107                              
harmonic(month, 2, 12)2  0.012 -0.021  0.135 -0.096                       
harmonic(month, 2, 12)3  0.142 -0.236  0.525 -0.075     0.027             
harmonic(month, 2, 12)4  0.044 -0.008  0.005 -0.072    -0.008    -0.005   
To:I(time - tpand)       0.046 -0.014 -0.724  0.181    -0.113    -0.510   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.086   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.8369609 -0.4648814  0.1807391  0.5648681  1.8998449 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Anxiety and Mood Disorders Level change 1.03 (0.99 to 1.08), p = 0.146
Norway Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p = 0.465
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,1) with zero mean 

Coefficients:
         ma1
      0.4467
s.e.  0.1845

sigma^2 = 0.9255:  log likelihood = -49.29
AIC=102.59   AICc=102.95   BIC=105.75
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 8.199699e-08 (Intr)
time        1.142621e-09 0     
Residual    1.603203e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.040261200 -0.315496102 -0.191533122 -0.567805396 -0.536486981 -0.304504715 
        Phi7         Phi8 
-0.007125384 -0.330958803 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -4.386350 0.011998102 28 -365.5870  0.0000
time                     0.020555 0.001293672 28   15.8886  0.0000
To                       0.862270 0.012148054 28   70.9801  0.0000
harmonic(month, 2, 12)1 -0.019470 0.007346871 28   -2.6501  0.0131
harmonic(month, 2, 12)2  0.014371 0.007697487 28    1.8670  0.0724
harmonic(month, 2, 12)3 -0.038992 0.008163618 28   -4.7763  0.0001
harmonic(month, 2, 12)4  0.012929 0.007501808 28    1.7234  0.0958
To:I(time - tpand)      -0.027869 0.001301932 28  -21.4055  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.962                                            
To                       0.721 -0.848                                     
harmonic(month, 2, 12)1  0.067 -0.031 -0.017                              
harmonic(month, 2, 12)2 -0.037  0.009  0.000 -0.143                       
harmonic(month, 2, 12)3  0.330 -0.320  0.160 -0.001     0.062             
harmonic(month, 2, 12)4 -0.036  0.031  0.013  0.039    -0.040    -0.135   
To:I(time - tpand)       0.920 -0.937  0.647  0.054     0.015     0.402   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.071   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.90576357 -0.32001339  0.08284602  0.63734932  1.68329103 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Anxiety and Mood Disorders Level change 2.37 (2.32 to 2.42), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.03 (0.99 to 1.08), p = 0.146
Norway Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p = 0.465
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.02
AIC=140.05   AICc=140.13   BIC=141.92
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.816090e-07 (Intr)
time        6.240325e-09 0     
Residual    1.297533e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.001913602  0.015605855 -0.105562923  0.195323152 -0.217124136 -0.085549952 
        Phi7         Phi8         Phi9        Phi10        Phi11 
 0.121795462 -0.075811142 -0.018776102 -0.084025656 -0.406016658 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.918005 0.02670459 40 -221.61006  0.0000
time                     0.006558 0.00174084 40    3.76723  0.0005
To                       0.268912 0.04630317 40    5.80763  0.0000
harmonic(month, 2, 12)1 -0.018097 0.01159880 40   -1.56028  0.1266
harmonic(month, 2, 12)2  0.011017 0.01097290 40    1.00406  0.3214
harmonic(month, 2, 12)3  0.014137 0.01234431 40    1.14522  0.2589
harmonic(month, 2, 12)4 -0.017386 0.01079676 40   -1.61028  0.1152
To:I(time - tpand)       0.014797 0.00268404 40    5.51279  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.930                                            
To                       0.546 -0.730                                     
harmonic(month, 2, 12)1 -0.125  0.104 -0.079                              
harmonic(month, 2, 12)2 -0.011 -0.026  0.069  0.014                       
harmonic(month, 2, 12)3  0.175 -0.243  0.410 -0.064     0.068             
harmonic(month, 2, 12)4 -0.020  0.034 -0.015 -0.068    -0.049    -0.038   
To:I(time - tpand)       0.403 -0.356 -0.304  0.065    -0.015    -0.248   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0197683 -0.7108163 -0.1890457  0.6745163  2.5908460 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Anxiety and Mood Disorders Level change 1.31 (1.2 to 1.43), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.01 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.37 (2.32 to 2.42), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.03 (0.99 to 1.08), p = 0.146
Norway Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p = 0.465
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.056:  log likelihood = -69.41
AIC=140.82   AICc=140.91   BIC=142.69
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.166741e-07 (Intr)
time        6.050360e-09 0     
Residual    2.654085e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.064913083  0.179053899  0.028747502  0.033770425  0.009799224 -0.611224188 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.3554689 0.015254590 40 -154.41050  0.0000
time                     0.0053116 0.000934854 40    5.68172  0.0000
To                       0.0632234 0.022699291 40    2.78526  0.0081
harmonic(month, 2, 12)1  0.0197997 0.018553170 40    1.06719  0.2923
harmonic(month, 2, 12)2 -0.0326646 0.004798943 40   -6.80662  0.0000
harmonic(month, 2, 12)3  0.0185238 0.018513949 40    1.00053  0.3231
harmonic(month, 2, 12)4  0.0080189 0.004682085 40    1.71268  0.0945
To:I(time - tpand)       0.0017940 0.001571156 40    1.14181  0.2603
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.430 -0.642                                     
harmonic(month, 2, 12)1 -0.114  0.078 -0.021                              
harmonic(month, 2, 12)2  0.045 -0.065  0.126 -0.077                       
harmonic(month, 2, 12)3 -0.066  0.008  0.117  0.039    -0.062             
harmonic(month, 2, 12)4  0.001  0.039 -0.059 -0.058     0.001    -0.026   
To:I(time - tpand)       0.493 -0.495 -0.239 -0.020    -0.002    -0.174   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.025   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.2734150 -0.6371616  0.0189445  0.6275732  2.1516448 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Anxiety and Mood Disorders Level change 1.07 (1.02 to 1.11), p = 0.008
Sweden Anxiety and Mood Disorders Trend change 1 (1 to 1), p = 0.26
Singapore Anxiety and Mood Disorders Level change 1.31 (1.2 to 1.43), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.01 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.37 (2.32 to 2.42), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.03 (0.99 to 1.08), p = 0.146
Norway Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p = 0.465
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Anxiety and Mood Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.057:  log likelihood = -69.43
AIC=140.86   AICc=140.95   BIC=142.73
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.975429e-07 (Intr)
time        4.862553e-09 0     
Residual    1.679667e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi 
-0.08886839 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                             Value   Std.Error DF    t-value p-value
(Intercept)             -2.2050829 0.014445159 40 -152.65203  0.0000
time                     0.0037004 0.000911117 40    4.06141  0.0002
To                      -0.0150370 0.022655472 40   -0.66372  0.5107
harmonic(month, 2, 12)1  0.0397706 0.007649806 40    5.19891  0.0000
harmonic(month, 2, 12)2  0.0000498 0.007727479 40    0.00645  0.9949
harmonic(month, 2, 12)3 -0.0262171 0.007835760 40   -3.34583  0.0018
harmonic(month, 2, 12)4 -0.0085746 0.007788471 40   -1.10093  0.2775
To:I(time - tpand)       0.0023442 0.001627803 40    1.44008  0.1576
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.872                                            
To                       0.368 -0.603                                     
harmonic(month, 2, 12)1 -0.139  0.097 -0.054                              
harmonic(month, 2, 12)2 -0.031 -0.002  0.031  0.008                       
harmonic(month, 2, 12)3  0.114 -0.156  0.256 -0.028    -0.008             
harmonic(month, 2, 12)4  0.026 -0.003 -0.019 -0.004    -0.013     0.013   
To:I(time - tpand)       0.451 -0.517 -0.233  0.043     0.022    -0.089   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.006   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0712237 -0.5105202  0.0815082  0.6517687  2.2740489 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Anxiety and Mood Disorders Level change 0.99 (0.94 to 1.03), p = 0.511
USA Anxiety and Mood Disorders Trend change 1 (1 to 1.01), p = 0.158
Sweden Anxiety and Mood Disorders Level change 1.07 (1.02 to 1.11), p = 0.008
Sweden Anxiety and Mood Disorders Trend change 1 (1 to 1), p = 0.26
Singapore Anxiety and Mood Disorders Level change 1.31 (1.2 to 1.43), p < 0.001
Singapore Anxiety and Mood Disorders Trend change 1.01 (1.01 to 1.02), p < 0.001
Peru Anxiety and Mood Disorders Level change 2.37 (2.32 to 2.42), p < 0.001
Peru Anxiety and Mood Disorders Trend change 0.97 (0.97 to 0.97), p < 0.001
Norway Anxiety and Mood Disorders Level change 1.03 (0.99 to 1.08), p = 0.146
Norway Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p = 0.465
China Anxiety and Mood Disorders Level change 2.06 (1.66 to 2.55), p < 0.001
China Anxiety and Mood Disorders Trend change 0.93 (0.91 to 0.95), p < 0.001
Canada Anxiety and Mood Disorders Level change 1.28 (1.22 to 1.35), p < 0.001
Canada Anxiety and Mood Disorders Trend change 0.99 (0.99 to 1), p < 0.001
Australia Anxiety and Mood Disorders Level change 1.1 (1.06 to 1.13), p < 0.001
Australia Anxiety and Mood Disorders Trend change 1 (0.99 to 1), p < 0.001
Argentina Anxiety and Mood Disorders Level change 1.23 (1.15 to 1.32), p < 0.001
Argentina Anxiety and Mood Disorders Trend change 1.01 (1 to 1.01), p = 0.032
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.23 (1.15 to 1.32), p < 0.001

Trend change

1.01 (1 to 1.01), p = 0.032

Australia

Level change

1.1 (1.06 to 1.13), p < 0.001

Trend change

1 (0.99 to 1), p < 0.001

Canada

Level change

1.28 (1.22 to 1.35), p < 0.001

Trend change

0.99 (0.99 to 1), p < 0.001

China

Level change

2.06 (1.66 to 2.55), p < 0.001

Trend change

0.93 (0.91 to 0.95), p < 0.001

Norway

Level change

1.03 (0.99 to 1.08), p = 0.146

Trend change

1 (0.99 to 1), p = 0.465

Peru

Level change

2.37 (2.32 to 2.42), p < 0.001

Trend change

0.97 (0.97 to 0.97), p < 0.001

Singapore

Level change

1.31 (1.2 to 1.43), p < 0.001

Trend change

1.01 (1.01 to 1.02), p < 0.001

Sweden

Level change

1.07 (1.02 to 1.11), p = 0.008

Trend change

1 (1 to 1), p = 0.26

USA

Level change

0.99 (0.94 to 1.03), p = 0.511

Trend change

1 (1 to 1.01), p = 0.158

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: ADHD and Eating Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.053:  log likelihood = -69.35
AIC=140.71   AICc=140.79   BIC=142.58
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.030284e-06 (Intr)
time        4.860784e-09 0     
Residual    1.136350e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5 
 0.2086722 -0.3016471  0.2464587 -0.2941351  0.2887645 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.732654 0.05065447 40 -132.91333  0.0000
time                    -0.012528 0.00317588 40   -3.94466  0.0003
To                      -0.609878 0.09397963 40   -6.48947  0.0000
harmonic(month, 2, 12)1 -0.009671 0.02475091 40   -0.39073  0.6981
harmonic(month, 2, 12)2  0.016647 0.03503865 40    0.47511  0.6373
harmonic(month, 2, 12)3  0.005227 0.02541504 40    0.20568  0.8381
harmonic(month, 2, 12)4  0.039373 0.03576907 40    1.10074  0.2776
To:I(time - tpand)       0.042875 0.00704214 40    6.08836  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.873                                            
To                       0.285 -0.447                                     
harmonic(month, 2, 12)1 -0.125  0.110 -0.069                              
harmonic(month, 2, 12)2 -0.006 -0.004 -0.003  0.026                       
harmonic(month, 2, 12)3  0.157 -0.179  0.246 -0.024     0.001             
harmonic(month, 2, 12)4  0.043 -0.031  0.025 -0.053    -0.004     0.059   
To:I(time - tpand)       0.383 -0.465 -0.461  0.049     0.044    -0.074   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.018   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.13503137 -0.73673819 -0.02335964  0.53907986  2.44884208 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.06:  log likelihood = -69.51
AIC=141.02   AICc=141.11   BIC=142.89
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.926940e-07 (Intr)
time        5.578233e-09 0     
Residual    9.885329e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.003395706 -0.143973483  0.116531888 -0.085661219 -0.154297294 -0.182782884 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -7.103822 0.03220364 40 -220.59068  0.0000
time                     0.005466 0.00196176 40    2.78608  0.0081
To                       0.167346 0.04203040 40    3.98154  0.0003
harmonic(month, 2, 12)1  0.011777 0.02130871 40    0.55267  0.5836
harmonic(month, 2, 12)2  0.056670 0.01340048 40    4.22892  0.0001
harmonic(month, 2, 12)3  0.006230 0.02196947 40    0.28358  0.7782
harmonic(month, 2, 12)4 -0.075719 0.01329962 40   -5.69333  0.0000
To:I(time - tpand)       0.025182 0.00271252 40    9.28376  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.906                                            
To                       0.483 -0.707                                     
harmonic(month, 2, 12)1 -0.091  0.079 -0.106                              
harmonic(month, 2, 12)2 -0.012 -0.018  0.054  0.033                       
harmonic(month, 2, 12)3  0.105 -0.107  0.163 -0.005     0.069             
harmonic(month, 2, 12)4  0.009  0.020 -0.017 -0.036     0.005    -0.002   
To:I(time - tpand)       0.576 -0.612 -0.013  0.103     0.016    -0.043   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.043   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7657617 -0.6750158 -0.0703690  0.3855582  2.5653491 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.062:  log likelihood = -69.55
AIC=141.1   AICc=141.18   BIC=142.97
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.150861e-07 (Intr)
time        2.997863e-09 0     
Residual    1.164268e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.231569970  0.090375265  0.003662638 -0.121718284 -0.672291908 -0.513239161 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.176022716 -0.038419639  0.066941390 -0.344043305 -0.343549326 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -5.696639 0.011395150 40 -499.9178  0.0000
time                     0.005198 0.000710662 40    7.3144  0.0000
To                       0.197386 0.017144556 40   11.5130  0.0000
harmonic(month, 2, 12)1  0.045546 0.030262861 40    1.5050  0.1402
harmonic(month, 2, 12)2 -0.025379 0.004927771 40   -5.1503  0.0000
harmonic(month, 2, 12)3  0.059021 0.029649979 40    1.9906  0.0534
harmonic(month, 2, 12)4 -0.026801 0.004841269 40   -5.5360  0.0000
To:I(time - tpand)       0.008191 0.001044768 40    7.8404  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.940                                            
To                       0.583 -0.749                                     
harmonic(month, 2, 12)1 -0.220  0.196 -0.143                              
harmonic(month, 2, 12)2  0.024 -0.041  0.077 -0.113                       
harmonic(month, 2, 12)3 -0.039  0.000  0.047  0.032    -0.017             
harmonic(month, 2, 12)4 -0.017  0.029 -0.007  0.013    -0.040    -0.084   
To:I(time - tpand)       0.419 -0.380 -0.248 -0.015    -0.008    -0.090   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.067   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7793011 -0.7656802 -0.1417781  0.5172337  2.6275920 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(2,0,0) with non-zero mean 

Coefficients:
          ar1      ar2     mean
      -0.6238  -0.6405  -0.1353
s.e.   0.1366   0.1323   0.0554

sigma^2 = 0.5729:  log likelihood = -38.95
AIC=85.9   AICc=87.24   BIC=92.12
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.218670e-08 (Intr)
time        7.312422e-10 0     
Residual    9.970359e-01       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.1916943 -0.4303591 -0.9999950 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -8.000370 0.4987412 26 -16.041126  0.0000
time                    -0.050454 0.0766652 26  -0.658107  0.5163
To                       1.194317 1.0869300 26   1.098798  0.2819
harmonic(month, 2, 12)1 -0.291896 0.2983494 26  -0.978369  0.3369
harmonic(month, 2, 12)2  0.141150 0.1907255 26   0.740070  0.4659
harmonic(month, 2, 12)3 -0.325445 0.1190147 26  -2.734494  0.0111
harmonic(month, 2, 12)4 -0.372931 0.1660647 26  -2.245698  0.0334
China_Jan19             -0.216377 0.9397007 26  -0.230262  0.8197
To:I(time - tpand)      -0.003886 0.0464791 26  -0.083609  0.9340
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.998                                            
To                       0.987 -0.989                                     
harmonic(month, 2, 12)1 -0.972  0.978 -0.978                              
harmonic(month, 2, 12)2 -0.585  0.589 -0.578  0.638                       
harmonic(month, 2, 12)3  0.791 -0.766  0.758 -0.725    -0.325             
harmonic(month, 2, 12)4  0.433 -0.396  0.376 -0.384    -0.203     0.506   
China_Jan19              0.995 -0.999  0.984 -0.977    -0.591     0.755   
To:I(time - tpand)      -0.655  0.666 -0.763  0.716     0.380    -0.433   
                        h(,2,12)4 Ch_J19
time                                    
To                                      
harmonic(month, 2, 12)1                 
harmonic(month, 2, 12)2                 
harmonic(month, 2, 12)3                 
harmonic(month, 2, 12)4                 
China_Jan19              0.389          
To:I(time - tpand)      -0.123    -0.658

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.3398132 -0.8588707 -0.1737197  0.7220485  2.4027515 

Number of Observations: 35
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China ADHD and Eating Disorders Level change 3.3 (0.48 to 22.65), p = 0.282
China ADHD and Eating Disorders Trend change 1 (0.92 to 1.08), p = 0.934
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,2) with zero mean 

Coefficients:
          ma1      ma2
      -0.3886  -0.3390
s.e.   0.1641   0.1677

sigma^2 = 0.8804:  log likelihood = -49.45
AIC=104.9   AICc=105.63   BIC=109.73
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.752741e-07 (Intr)
time        8.123967e-09 0     
Residual    3.540351e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
-0.2481453 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.437851 0.01737177 29 -313.02810  0.0000
time                     0.004421 0.00107360 29    4.11838  0.0003
To                       0.022472 0.03591605 29    0.62569  0.5364
harmonic(month, 2, 12)1 -0.020578 0.01016595 29   -2.02418  0.0522
harmonic(month, 2, 12)2 -0.038605 0.01042114 29   -3.70452  0.0009
harmonic(month, 2, 12)3  0.023078 0.01199480 29    1.92404  0.0642
harmonic(month, 2, 12)4  0.003412 0.01051435 29    0.32453  0.7479
To:I(time - tpand)       0.017499 0.00546674 29    3.20108  0.0033
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.882                                            
To                       0.332 -0.531                                     
harmonic(month, 2, 12)1 -0.132  0.104 -0.091                              
harmonic(month, 2, 12)2  0.018 -0.027  0.144 -0.097                       
harmonic(month, 2, 12)3  0.145 -0.234  0.536 -0.073     0.059             
harmonic(month, 2, 12)4  0.039 -0.004  0.009 -0.107    -0.007    -0.002   
To:I(time - tpand)       0.057 -0.027 -0.720  0.165    -0.125    -0.533   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.096   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.71781161 -0.41143168  0.08312172  0.73545165  1.35847036 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway ADHD and Eating Disorders Level change 1.02 (0.96 to 1.09), p = 0.536
Norway ADHD and Eating Disorders Trend change 1.02 (1.01 to 1.03), p = 0.003
China ADHD and Eating Disorders Level change 3.3 (0.48 to 22.65), p = 0.282
China ADHD and Eating Disorders Trend change 1 (0.92 to 1.08), p = 0.934
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.14:  log likelihood = -53.45
AIC=108.89   AICc=109.01   BIC=110.48
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 9.302764e-07 (Intr)
time        1.701648e-08 0     
Residual    6.580178e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.279078902 -0.009965086  0.535715939 -0.070960160 -0.017905881 -0.236486660 
        Phi7         Phi8 
-0.080990933 -0.307143196 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.930493 0.05637759 28 -122.92991  0.0000
time                    -0.009845 0.00603469 28   -1.63136  0.1140
To                       0.439439 0.06762007 28    6.49865  0.0000
harmonic(month, 2, 12)1  0.016384 0.01589170 28    1.03100  0.3114
harmonic(month, 2, 12)2  0.002968 0.00921602 28    0.32200  0.7498
harmonic(month, 2, 12)3 -0.006433 0.01723774 28   -0.37318  0.7118
harmonic(month, 2, 12)4  0.025206 0.00887970 28    2.83863  0.0083
To:I(time - tpand)       0.012483 0.00753452 28    1.65678  0.1087
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.928                                            
To                       0.363 -0.596                                     
harmonic(month, 2, 12)1  0.054 -0.134  0.291                              
harmonic(month, 2, 12)2  0.009 -0.099  0.219  0.171                       
harmonic(month, 2, 12)3 -0.168  0.037  0.351  0.081     0.119             
harmonic(month, 2, 12)4 -0.043  0.075 -0.093 -0.139    -0.036     0.049   
To:I(time - tpand)       0.853 -0.861  0.155  0.010     0.036    -0.269   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.061   

Standardized Within-Group Residuals:
          Min            Q1           Med            Q3           Max 
-1.3635284852 -0.5423390636  0.0008120699  0.7256630045  1.9996501451 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru ADHD and Eating Disorders Level change 1.55 (1.37 to 1.75), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.01 (1 to 1.03), p = 0.109
Norway ADHD and Eating Disorders Level change 1.02 (0.96 to 1.09), p = 0.536
Norway ADHD and Eating Disorders Trend change 1.02 (1.01 to 1.03), p = 0.003
China ADHD and Eating Disorders Level change 3.3 (0.48 to 22.65), p = 0.282
China ADHD and Eating Disorders Trend change 1 (0.92 to 1.08), p = 0.934
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: ADHD and Eating Disorders

Modeling ADHD and Eating Disorders for Singapore is not appropiate due to missing data in numerator.

Code
fake <- data.frame(x = seq(1, 48, 1), 
                   y = seq(1, 48, 1))

fake |> 
  ggplot(aes(x = x, y = y)) + 
  theme_void() + 
  annotate(geom = "text", 
           x = 24, 
           y = 24, 
           label = "Not calculable", 
           color = "black", 
           size = 6) -> plot7
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.058:  log likelihood = -69.46
AIC=140.91   AICc=141   BIC=142.78
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.206623e-06 (Intr)
time        4.169251e-09 0     
Residual    9.955434e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
         Phi1          Phi2          Phi3          Phi4          Phi5 
 0.0227672636  0.1103830095 -0.0712207714 -0.0038789337  0.0668937035 
         Phi6 
 0.0006282233 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.636977 0.06662554 40 -99.61611  0.0000
time                     0.027393 0.00374810 40   7.30864  0.0000
To                       0.155253 0.07494844 40   2.07147  0.0448
harmonic(month, 2, 12)1  0.029217 0.02491643 40   1.17261  0.2479
harmonic(month, 2, 12)2  0.004066 0.02594886 40   0.15670  0.8763
harmonic(month, 2, 12)3 -0.009684 0.02614512 40  -0.37040  0.7130
harmonic(month, 2, 12)4 -0.003127 0.02529639 40  -0.12361  0.9022
To:I(time - tpand)      -0.015819 0.00561907 40  -2.81527  0.0075
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.894                                            
To                       0.366 -0.605                                     
harmonic(month, 2, 12)1 -0.061  0.017 -0.013                              
harmonic(month, 2, 12)2  0.013 -0.032  0.074 -0.025                       
harmonic(month, 2, 12)3  0.081 -0.150  0.303  0.032     0.019             
harmonic(month, 2, 12)4  0.007  0.029 -0.038 -0.049     0.007    -0.037   
To:I(time - tpand)       0.586 -0.660 -0.051  0.091     0.029    -0.095   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.041   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.62368566 -0.58839218  0.08159998  0.53390529  2.35929661 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden ADHD and Eating Disorders Level change 1.17 (1.02 to 1.34), p = 0.045
Sweden ADHD and Eating Disorders Trend change 0.98 (0.97 to 0.99), p = 0.008
Peru ADHD and Eating Disorders Level change 1.55 (1.37 to 1.75), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.01 (1 to 1.03), p = 0.109
Norway ADHD and Eating Disorders Level change 1.02 (0.96 to 1.09), p = 0.536
Norway ADHD and Eating Disorders Trend change 1.02 (1.01 to 1.03), p = 0.003
China ADHD and Eating Disorders Level change 3.3 (0.48 to 22.65), p = 0.282
China ADHD and Eating Disorders Trend change 1 (0.92 to 1.08), p = 0.934
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: ADHD and Eating Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.027:  log likelihood = -68.74
AIC=139.48   AICc=139.56   BIC=141.35
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.945466e-07 (Intr)
time        2.310128e-09 0     
Residual    1.172404e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.1612528 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.349074 0.06012418 40 -88.96709  0.0000
time                     0.005891 0.00374453 40   1.57332  0.1235
To                      -0.115033 0.09161005 40  -1.25568  0.2165
harmonic(month, 2, 12)1  0.028909 0.03091744 40   0.93505  0.3554
harmonic(month, 2, 12)2 -0.010696 0.02819870 40  -0.37931  0.7065
harmonic(month, 2, 12)3  0.006083 0.03117191 40   0.19515  0.8463
harmonic(month, 2, 12)4 -0.001786 0.02817465 40  -0.06340  0.9498
To:I(time - tpand)       0.013081 0.00665009 40   1.96698  0.0561
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.868                                            
To                       0.341 -0.579                                     
harmonic(month, 2, 12)1 -0.130  0.089 -0.045                              
harmonic(month, 2, 12)2 -0.032 -0.001  0.038 -0.012                       
harmonic(month, 2, 12)3  0.076 -0.130  0.249 -0.025    -0.001             
harmonic(month, 2, 12)4  0.005  0.019 -0.027 -0.023    -0.023    -0.026   
To:I(time - tpand)       0.473 -0.549 -0.228  0.041     0.015    -0.110   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.032   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.60039962 -0.92497281  0.01616029  0.69575076  2.06241036 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA ADHD and Eating Disorders Level change 0.89 (0.75 to 1.06), p = 0.217
USA ADHD and Eating Disorders Trend change 1.01 (1 to 1.03), p = 0.056
Sweden ADHD and Eating Disorders Level change 1.17 (1.02 to 1.34), p = 0.045
Sweden ADHD and Eating Disorders Trend change 0.98 (0.97 to 0.99), p = 0.008
Peru ADHD and Eating Disorders Level change 1.55 (1.37 to 1.75), p < 0.001
Peru ADHD and Eating Disorders Trend change 1.01 (1 to 1.03), p = 0.109
Norway ADHD and Eating Disorders Level change 1.02 (0.96 to 1.09), p = 0.536
Norway ADHD and Eating Disorders Trend change 1.02 (1.01 to 1.03), p = 0.003
China ADHD and Eating Disorders Level change 3.3 (0.48 to 22.65), p = 0.282
China ADHD and Eating Disorders Trend change 1 (0.92 to 1.08), p = 0.934
Canada ADHD and Eating Disorders Level change 1.22 (1.18 to 1.26), p < 0.001
Canada ADHD and Eating Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Australia ADHD and Eating Disorders Level change 1.18 (1.09 to 1.28), p < 0.001
Australia ADHD and Eating Disorders Trend change 1.03 (1.02 to 1.03), p < 0.001
Argentina ADHD and Eating Disorders Level change 0.54 (0.46 to 0.65), p < 0.001
Argentina ADHD and Eating Disorders Trend change 1.04 (1.03 to 1.06), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

0.54 (0.46 to 0.65), p < 0.001

Trend change

1.04 (1.03 to 1.06), p < 0.001

Australia

Level change

1.18 (1.09 to 1.28), p < 0.001

Trend change

1.03 (1.02 to 1.03), p < 0.001

Canada

Level change

1.22 (1.18 to 1.26), p < 0.001

Trend change

1.01 (1.01 to 1.01), p < 0.001

China

Level change

3.3 (0.48 to 22.65), p = 0.282

Trend change

1 (0.92 to 1.08), p = 0.934

Norway

Level change

1.02 (0.96 to 1.09), p = 0.536

Trend change

1.02 (1.01 to 1.03), p = 0.003

Peru

Level change

1.55 (1.37 to 1.75), p < 0.001

Trend change

1.01 (1 to 1.03), p = 0.109

Sweden

Level change

1.17 (1.02 to 1.34), p = 0.045

Trend change

0.98 (0.97 to 0.99), p = 0.008

USA

Level change

0.89 (0.75 to 1.06), p = 0.217

Trend change

1.01 (1 to 1.03), p = 0.056

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Bipolar, Schizophrenia and other Psycotic Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.05:  log likelihood = -69.28
AIC=140.57   AICc=140.65   BIC=142.44
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.115527e-06 (Intr)
time        7.073196e-09 0     
Residual    1.613305e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.30559085  0.15212461 -0.03866796  0.07274164 -0.16423940 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.857159 0.06059601 40 -96.65916  0.0000
time                     0.000564 0.00368181 40   0.15313  0.8791
To                      -0.026229 0.08977771 40  -0.29215  0.7717
harmonic(month, 2, 12)1 -0.046368 0.03434291 40  -1.35014  0.1846
harmonic(month, 2, 12)2  0.033049 0.01894657 40   1.74433  0.0888
harmonic(month, 2, 12)3  0.050115 0.03400383 40   1.47379  0.1484
harmonic(month, 2, 12)4  0.047282 0.01909303 40   2.47642  0.0176
To:I(time - tpand)      -0.009604 0.00714618 40  -1.34400  0.1865
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.891                                            
To                       0.331 -0.526                                     
harmonic(month, 2, 12)1 -0.075  0.070 -0.024                              
harmonic(month, 2, 12)2  0.003 -0.035  0.106  0.018                       
harmonic(month, 2, 12)3  0.064 -0.096  0.201 -0.012     0.035             
harmonic(month, 2, 12)4 -0.019  0.038 -0.053 -0.091    -0.030     0.006   
To:I(time - tpand)       0.472 -0.534 -0.297  0.018    -0.022    -0.099   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.029   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.17922288 -0.50827685 -0.01805037  0.58991116  2.43055895 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.055:  log likelihood = -69.4
AIC=140.79   AICc=140.88   BIC=142.66
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.860832e-07 (Intr)
time        8.155855e-09 0     
Residual    1.231073e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
-0.2078001 -0.2798133 -0.2812477 -0.1378737 -0.3505320 -0.1518825 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -6.227006 0.016763686 40 -371.4580  0.0000
time                    -0.009128 0.001054210 40   -8.6588  0.0000
To                       0.175503 0.024864598 40    7.0584  0.0000
harmonic(month, 2, 12)1  0.002770 0.013127300 40    0.2110  0.8340
harmonic(month, 2, 12)2  0.055403 0.017059060 40    3.2477  0.0024
harmonic(month, 2, 12)3  0.104863 0.013863517 40    7.5640  0.0000
harmonic(month, 2, 12)4 -0.015657 0.017228631 40   -0.9088  0.3689
To:I(time - tpand)       0.009298 0.001663055 40    5.5908  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.908                                            
To                       0.487 -0.696                                     
harmonic(month, 2, 12)1 -0.065  0.059 -0.082                              
harmonic(month, 2, 12)2 -0.057  0.038 -0.006 -0.067                       
harmonic(month, 2, 12)3  0.177 -0.186  0.165  0.006     0.010             
harmonic(month, 2, 12)4  0.019 -0.005  0.003  0.016     0.003    -0.051   
To:I(time - tpand)       0.477 -0.492 -0.166  0.089    -0.004     0.070   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.019   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.90709615 -0.71018944 -0.02973146  0.64113090  2.58399485 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.14:  log likelihood = -71.25
AIC=144.5   AICc=144.59   BIC=146.37
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.567990e-07 (Intr)
time        2.782674e-09 0     
Residual    1.213303e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
 0.052211983 -0.051530676 -0.016163886 -0.009680077 -0.221763710 -0.210492180 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.246061227  0.016816994 -0.043892718 -0.103230614 -0.359031530 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -4.872828 0.014390263 40 -338.6198  0.0000
time                    -0.003187 0.000940753 40   -3.3881  0.0016
To                       0.245772 0.025521703 40    9.6299  0.0000
harmonic(month, 2, 12)1  0.039214 0.013495466 40    2.9057  0.0059
harmonic(month, 2, 12)2 -0.009980 0.006875658 40   -1.4515  0.1544
harmonic(month, 2, 12)3 -0.012589 0.013327109 40   -0.9446  0.3505
harmonic(month, 2, 12)4 -0.012443 0.006856174 40   -1.8149  0.0770
To:I(time - tpand)      -0.010327 0.001578799 40   -6.5407  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.934                                            
To                       0.571 -0.743                                     
harmonic(month, 2, 12)1 -0.197  0.168 -0.119                              
harmonic(month, 2, 12)2 -0.021 -0.004  0.053  0.041                       
harmonic(month, 2, 12)3  0.064 -0.095  0.218 -0.025    -0.014             
harmonic(month, 2, 12)4 -0.011  0.024 -0.020 -0.014    -0.037     0.028   
To:I(time - tpand)       0.313 -0.260 -0.374  0.028    -0.021    -0.212   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.034   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.86936210 -0.67055189 -0.07947393  0.56295858  3.55526027 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.138:  log likelihood = -51.92
AIC=105.84   AICc=105.97   BIC=107.4
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.724380e-06 (Intr)
time        3.714011e-08 0     
Residual    9.754357e-01       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.5339471 -0.2535041  0.4234572 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -8.646244 0.3283633 26 -26.331337  0.0000
time                     0.091554 0.0422824 26   2.165301  0.0397
To                      -0.714959 0.5911325 26  -1.209473  0.2374
harmonic(month, 2, 12)1  0.385359 0.1738213 26   2.216985  0.0356
harmonic(month, 2, 12)2 -0.019719 0.1037543 26  -0.190051  0.8507
harmonic(month, 2, 12)3 -0.192890 0.1318128 26  -1.463361  0.1554
harmonic(month, 2, 12)4 -0.241902 0.1186638 26  -2.038552  0.0518
China_Jan19              0.199904 0.5658406 26   0.353287  0.7267
To:I(time - tpand)       0.022684 0.0471967 26   0.480617  0.6348
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.865                                            
To                       0.781 -0.895                                     
harmonic(month, 2, 12)1 -0.682  0.778 -0.821                              
harmonic(month, 2, 12)2 -0.190  0.239 -0.262  0.341                       
harmonic(month, 2, 12)3  0.609 -0.685  0.726 -0.564    -0.140             
harmonic(month, 2, 12)4  0.490 -0.544  0.511 -0.425    -0.121     0.413   
China_Jan19              0.669 -0.943  0.817 -0.737    -0.238     0.632   
To:I(time - tpand)      -0.121  0.126 -0.495  0.486     0.241    -0.294   
                        h(,2,12)4 Ch_J19
time                                    
To                                      
harmonic(month, 2, 12)1                 
harmonic(month, 2, 12)2                 
harmonic(month, 2, 12)3                 
harmonic(month, 2, 12)4                 
China_Jan19              0.500          
To:I(time - tpand)      -0.072    -0.115

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.0368427 -0.3510108 -0.1310524  0.5417462  3.0681762 

Number of Observations: 35
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.161:  log likelihood = -55.26
AIC=112.52   AICc=112.63   BIC=114.13
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.493706e-07 (Intr)
time        8.503307e-09 0     
Residual    2.710182e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
0.04862081 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -5.083395 0.014909481 29 -340.9505  0.0000
time                    -0.000227 0.000927664 29   -0.2452  0.8080
To                       0.095570 0.029793628 29    3.2077  0.0033
harmonic(month, 2, 12)1 -0.032092 0.008910982 29   -3.6014  0.0012
harmonic(month, 2, 12)2 -0.010479 0.008451823 29   -1.2399  0.2250
harmonic(month, 2, 12)3 -0.016400 0.010101328 29   -1.6236  0.1153
harmonic(month, 2, 12)4  0.019607 0.008529659 29    2.2987  0.0289
To:I(time - tpand)      -0.007810 0.004755218 29   -1.6425  0.1113
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.874                                            
To                       0.300 -0.498                                     
harmonic(month, 2, 12)1 -0.134  0.105 -0.086                              
harmonic(month, 2, 12)2 -0.010 -0.005  0.148 -0.077                       
harmonic(month, 2, 12)3  0.107 -0.178  0.487 -0.073     0.041             
harmonic(month, 2, 12)4  0.012  0.020 -0.022 -0.087    -0.014     0.001   
To:I(time - tpand)       0.105 -0.100 -0.680  0.149    -0.143    -0.488   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.070   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.95897063 -0.66182331 -0.09292903  0.61445310  2.60104802 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.1 (1.04 to 1.16), p = 0.003
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.111
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.6861
s.e.  0.1269

sigma^2 = 0.5996:  log likelihood = -41.69
AIC=87.37   AICc=87.74   BIC=90.54
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.701158e-07 (Intr)
time        5.173771e-09 0     
Residual    1.087706e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.05275314 -0.06674591  0.25819642  0.21504404  0.24876960 -0.47642344 
       Phi7        Phi8 
-0.34253943 -0.44335494 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -6.038378 0.023360971 28 -258.48145  0.0000
time                     0.017219 0.002535756 28    6.79029  0.0000
To                       0.909534 0.024833356 28   36.62548  0.0000
harmonic(month, 2, 12)1  0.001731 0.006237965 28    0.27748  0.7835
harmonic(month, 2, 12)2 -0.000781 0.003383320 28   -0.23073  0.8192
harmonic(month, 2, 12)3 -0.066316 0.006848736 28   -9.68294  0.0000
harmonic(month, 2, 12)4  0.019629 0.003322768 28    5.90750  0.0000
To:I(time - tpand)      -0.012276 0.002620423 28   -4.68470  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.954                                            
To                       0.635 -0.802                                     
harmonic(month, 2, 12)1  0.011 -0.038  0.103                              
harmonic(month, 2, 12)2  0.102 -0.147  0.174  0.192                       
harmonic(month, 2, 12)3 -0.013 -0.073  0.315  0.004     0.081             
harmonic(month, 2, 12)4 -0.020  0.050 -0.100 -0.099     0.011     0.063   
To:I(time - tpand)       0.919 -0.927  0.555  0.009     0.138    -0.107   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.6045345 -0.5761636 -0.1118516  1.1086781  1.7005916 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.1 (1.04 to 1.16), p = 0.003
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.111
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
Error in lme.formula(fixed = zz ~ time + To + harmonic(month, 2, 12) + : nlminb problem, convergence error code = 1
  message = iteration limit reached without convergence (10)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.6861
s.e.  0.1269

sigma^2 = 0.5996:  log likelihood = -41.69
AIC=87.37   AICc=87.74   BIC=90.54
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 3.701158e-07 (Intr)
time        5.173771e-09 0     
Residual    1.087706e+01       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.05275314 -0.06674591  0.25819642  0.21504404  0.24876960 -0.47642344 
       Phi7        Phi8 
-0.34253943 -0.44335494 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF    t-value p-value
(Intercept)             -6.038378 0.023360971 28 -258.48145  0.0000
time                     0.017219 0.002535756 28    6.79029  0.0000
To                       0.909534 0.024833356 28   36.62548  0.0000
harmonic(month, 2, 12)1  0.001731 0.006237965 28    0.27748  0.7835
harmonic(month, 2, 12)2 -0.000781 0.003383320 28   -0.23073  0.8192
harmonic(month, 2, 12)3 -0.066316 0.006848736 28   -9.68294  0.0000
harmonic(month, 2, 12)4  0.019629 0.003322768 28    5.90750  0.0000
To:I(time - tpand)      -0.012276 0.002620423 28   -4.68470  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.954                                            
To                       0.635 -0.802                                     
harmonic(month, 2, 12)1  0.011 -0.038  0.103                              
harmonic(month, 2, 12)2  0.102 -0.147  0.174  0.192                       
harmonic(month, 2, 12)3 -0.013 -0.073  0.315  0.004     0.081             
harmonic(month, 2, 12)4 -0.020  0.050 -0.100 -0.099     0.011     0.063   
To:I(time - tpand)       0.919 -0.927  0.555  0.009     0.138    -0.107   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.6045345 -0.5761636 -0.1118516  1.1086781  1.7005916 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.1 (1.04 to 1.16), p = 0.003
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.111
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,1) with zero mean 

Coefficients:
          ar1     ma1
      -0.8750  0.6667
s.e.   0.1079  0.1627

sigma^2 = 0.9468:  log likelihood = -65.92
AIC=137.84   AICc=138.38   BIC=143.45
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.422801e-07 (Intr)
time        1.929600e-09 0     
Residual    9.396682e-01       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.24172660  0.01165474 -0.21396291  0.10942432 -0.07856218 -0.02985355 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -7.270242 0.05973683 40 -121.70451  0.0000
time                    -0.007007 0.00377229 40   -1.85740  0.0706
To                       0.376296 0.09182771 40    4.09785  0.0002
harmonic(month, 2, 12)1  0.080200 0.03356108 40    2.38967  0.0217
harmonic(month, 2, 12)2  0.056996 0.03828835 40    1.48860  0.1444
harmonic(month, 2, 12)3 -0.021115 0.03628684 40   -0.58188  0.5639
harmonic(month, 2, 12)4 -0.054218 0.03821502 40   -1.41875  0.1637
To:I(time - tpand)       0.004866 0.00627140 40    0.77589  0.4424
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.879                                            
To                       0.412 -0.653                                     
harmonic(month, 2, 12)1 -0.150  0.097 -0.075                              
harmonic(month, 2, 12)2 -0.030  0.011  0.019 -0.023                       
harmonic(month, 2, 12)3  0.113 -0.159  0.246  0.001    -0.052             
harmonic(month, 2, 12)4  0.061 -0.015 -0.013  0.013     0.015     0.006   
To:I(time - tpand)       0.461 -0.512 -0.184  0.064     0.024    -0.074   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.006   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.26471208 -0.73772384  0.01977432  0.76879113  3.31496792 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.46 (1.23 to 1.73), p < 0.001
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1 (0.99 to 1.02), p = 0.442
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.1 (1.04 to 1.16), p = 0.003
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.111
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Bipolar, Schizophrenia and other Psycotic Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.034:  log likelihood = -68.92
AIC=139.83   AICc=139.92   BIC=141.7
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 8.023230e-07 (Intr)
time        4.450071e-09 0     
Residual    1.295512e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
0.01478648 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -4.911701 0.04715513 40 -104.16047  0.0000
time                     0.005274 0.00295463 40    1.78507  0.0818
To                      -0.170193 0.07610043 40   -2.23643  0.0310
harmonic(month, 2, 12)1  0.026375 0.02584751 40    1.02042  0.3137
harmonic(month, 2, 12)2  0.025584 0.02500457 40    1.02318  0.3124
harmonic(month, 2, 12)3 -0.018818 0.02589919 40   -0.72660  0.4717
harmonic(month, 2, 12)4  0.037928 0.02519107 40    1.50561  0.1400
To:I(time - tpand)      -0.002797 0.00561238 40   -0.49842  0.6209
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.871                                            
To                       0.346 -0.572                                     
harmonic(month, 2, 12)1 -0.137  0.098 -0.049                              
harmonic(month, 2, 12)2 -0.042  0.001  0.032 -0.004                       
harmonic(month, 2, 12)3  0.108 -0.155  0.255 -0.046    -0.015             
harmonic(month, 2, 12)4  0.006  0.006 -0.024 -0.004    -0.018     0.002   
To:I(time - tpand)       0.429 -0.493 -0.291  0.038     0.017    -0.095   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.011   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.3096678 -0.6726997 -0.1992589  0.8695448  2.0220097 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.84 (0.73 to 0.97), p = 0.031
USA Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1 (0.99 to 1.01), p = 0.621
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.46 (1.23 to 1.73), p < 0.001
Sweden Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1 (0.99 to 1.02), p = 0.442
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Singapore Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Level change 2.48 (2.37 to 2.6), p < 0.001
Peru Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Norway Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.1 (1.04 to 1.16), p = 0.003
Norway Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.111
China Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.49 (0.17 to 1.39), p = 0.237
China Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.02 (0.94 to 1.11), p = 0.635
Canada Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.28 (1.22 to 1.34), p < 0.001
Canada Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.99 to 0.99), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Level change 1.19 (1.14 to 1.25), p < 0.001
Australia Bipolar, Schizophrenia and other Psycotic Disorders Trend change 1.01 (1.01 to 1.01), p < 0.001
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Level change 0.97 (0.83 to 1.15), p = 0.772
Argentina Bipolar, Schizophrenia and other Psycotic Disorders Trend change 0.99 (0.98 to 1), p = 0.187
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

0.97 (0.83 to 1.15), p = 0.772

Trend change

0.99 (0.98 to 1), p = 0.187

Australia

Level change

1.19 (1.14 to 1.25), p < 0.001

Trend change

1.01 (1.01 to 1.01), p < 0.001

Canada

Level change

1.28 (1.22 to 1.34), p < 0.001

Trend change

0.99 (0.99 to 0.99), p < 0.001

China

Level change

0.49 (0.17 to 1.39), p = 0.237

Trend change

1.02 (0.94 to 1.11), p = 0.635

Norway

Level change

1.1 (1.04 to 1.16), p = 0.003

Trend change

0.99 (0.98 to 1), p = 0.111

Peru

Level change

2.48 (2.37 to 2.6), p < 0.001

Trend change

0.99 (0.98 to 0.99), p < 0.001

Singapore

Level change

2.48 (2.37 to 2.6), p < 0.001

Trend change

0.99 (0.98 to 0.99), p < 0.001

Sweden

Level change

1.46 (1.23 to 1.73), p < 0.001

Trend change

1 (0.99 to 1.02), p = 0.442

USA

Level change

0.84 (0.73 to 0.97), p = 0.031

Trend change

1 (0.99 to 1.01), p = 0.621

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Dementia

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.044:  log likelihood = -69.15
AIC=140.29   AICc=140.38   BIC=142.16
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.217327e-06 (Intr)
time        3.422500e-09 0     
Residual    9.504926e-01       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5 
 0.079858823 -0.027478945 -0.036011936  0.193426314  0.003653354 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -9.770843 0.19976572 40 -48.91151  0.0000
time                     0.017341 0.01131693 40   1.53228  0.1333
To                       0.173113 0.23750621 40   0.72888  0.4703
harmonic(month, 2, 12)1 -0.166741 0.07449669 40  -2.23823  0.0308
harmonic(month, 2, 12)2  0.190630 0.07305487 40   2.60941  0.0127
harmonic(month, 2, 12)3  0.204402 0.07282634 40   2.80670  0.0077
harmonic(month, 2, 12)4  0.119413 0.07399398 40   1.61382  0.1144
To:I(time - tpand)      -0.022431 0.01871362 40  -1.19864  0.2377
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.894                                            
To                       0.332 -0.546                                     
harmonic(month, 2, 12)1 -0.007  0.033 -0.010                              
harmonic(month, 2, 12)2 -0.032 -0.034  0.066 -0.044                       
harmonic(month, 2, 12)3  0.078 -0.167  0.310 -0.081     0.129             
harmonic(month, 2, 12)4  0.005 -0.010 -0.021 -0.155    -0.004    -0.031   
To:I(time - tpand)       0.552 -0.625 -0.165  0.051     0.031    -0.077   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.001   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.03752800 -0.65682799  0.05414143  0.62092604  2.38632745 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.071:  log likelihood = -69.75
AIC=141.5   AICc=141.59   BIC=143.38
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.704559e-07 (Intr)
time        2.503493e-09 0     
Residual    1.093167e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6 
-0.4274509 -0.1682413 -0.1057179 -0.4868659 -0.3008830 -0.3109109 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -7.541000 0.02267641 40 -332.5483  0.0000
time                    -0.010959 0.00142880 40   -7.6702  0.0000
To                       0.640115 0.03187098 40   20.0846  0.0000
harmonic(month, 2, 12)1 -0.004788 0.01932944 40   -0.2477  0.8056
harmonic(month, 2, 12)2  0.056615 0.01919910 40    2.9488  0.0053
harmonic(month, 2, 12)3  0.145151 0.02029223 40    7.1530  0.0000
harmonic(month, 2, 12)4 -0.019122 0.01944548 40   -0.9834  0.3313
To:I(time - tpand)      -0.016649 0.00214148 40   -7.7745  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.910                                            
To                       0.513 -0.730                                     
harmonic(month, 2, 12)1 -0.036  0.030 -0.032                              
harmonic(month, 2, 12)2 -0.065  0.047  0.010 -0.068                       
harmonic(month, 2, 12)3  0.150 -0.156  0.159  0.014     0.001             
harmonic(month, 2, 12)4  0.014 -0.001 -0.007  0.014    -0.007    -0.059   
To:I(time - tpand)       0.506 -0.519 -0.083  0.056    -0.043     0.041   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.014   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.5983404 -0.7855670  0.1079683  0.7721604  2.5456289 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.04:  log likelihood = -69.06
AIC=140.11   AICc=140.2   BIC=141.98
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.462084e-07 (Intr)
time        2.410179e-09 0     
Residual    1.234109e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.42762688 -0.22813088 -0.36786903 -0.63137236 -0.57470945 -0.14040098 
       Phi7        Phi8        Phi9       Phi10       Phi11 
-0.16444596 -0.42194315 -0.41146901  0.09670013 -0.08095897 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -5.748068 0.008654960 40 -664.1358  0.0000
time                     0.002535 0.000541615 40    4.6800  0.0000
To                       0.376361 0.012346552 40   30.4830  0.0000
harmonic(month, 2, 12)1 -0.013784 0.012955295 40   -1.0640  0.2937
harmonic(month, 2, 12)2  0.004447 0.024306807 40    0.1830  0.8558
harmonic(month, 2, 12)3 -0.016437 0.013334124 40   -1.2327  0.2249
harmonic(month, 2, 12)4  0.023569 0.024129233 40    0.9768  0.3346
To:I(time - tpand)      -0.000096 0.000792550 40   -0.1215  0.9039
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.928                                            
To                       0.551 -0.739                                     
harmonic(month, 2, 12)1 -0.017  0.029 -0.024                              
harmonic(month, 2, 12)2 -0.014  0.044 -0.076 -0.057                       
harmonic(month, 2, 12)3  0.136 -0.102  0.024  0.024     0.037             
harmonic(month, 2, 12)4  0.026 -0.051  0.041  0.015    -0.002     0.005   
To:I(time - tpand)       0.496 -0.476 -0.145  0.019     0.063     0.148   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.031   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.13782001 -0.57595325  0.03470004  0.63266532  2.29286003 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 0.9372:  log likelihood = -48.53
AIC=99.06   AICc=99.18   BIC=100.61
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 2.879067e-07 (Intr)
time        6.678087e-09 0     
Residual    8.444526e-01       

Correlation Structure: ARMA(2,1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2     Theta1 
-0.1322458 -0.4808931 -0.9999934 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + China_Jan19 + offset(log(denominator)) 
                            Value Std.Error DF    t-value p-value
(Intercept)             -8.750221 0.3118162 26 -28.062114  0.0000
time                    -0.207864 0.0505576 26  -4.111437  0.0003
To                       3.087399 0.7858162 26   3.928907  0.0006
harmonic(month, 2, 12)1 -0.726801 0.2497189 26  -2.910475  0.0073
harmonic(month, 2, 12)2  0.012300 0.1943569 26   0.063285  0.9500
harmonic(month, 2, 12)3  0.523236 0.0873811 26   5.987972  0.0000
harmonic(month, 2, 12)4  0.443643 0.1686977 26   2.629810  0.0142
China_Jan19              3.788662 0.6334279 26   5.981205  0.0000
To:I(time - tpand)       0.037305 0.0442711 26   0.842641  0.4071
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.985                                            
To                       0.974 -0.984                                     
harmonic(month, 2, 12)1 -0.944  0.956 -0.965                              
harmonic(month, 2, 12)2 -0.779  0.745 -0.688  0.644                       
harmonic(month, 2, 12)3  0.685 -0.702  0.710 -0.696    -0.524             
harmonic(month, 2, 12)4  0.142 -0.172  0.140 -0.180    -0.203     0.234   
China_Jan19              0.964 -0.995  0.973 -0.947    -0.731     0.694   
To:I(time - tpand)      -0.758  0.756 -0.852  0.828     0.410    -0.528   
                        h(,2,12)4 Ch_J19
time                                    
To                                      
harmonic(month, 2, 12)1                 
harmonic(month, 2, 12)2                 
harmonic(month, 2, 12)3                 
harmonic(month, 2, 12)4                 
China_Jan19              0.172          
To:I(time - tpand)       0.036    -0.737

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.4603591 -0.6920983 -0.4583805  0.4471792  2.3278610 

Number of Observations: 35
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.419:  log likelihood = -58.97
AIC=119.94   AICc=120.06   BIC=121.55
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 6.600931e-07 (Intr)
time        6.138709e-09 0     
Residual    3.107368e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi 
-0.2667012 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.198588 0.02235127 29 -277.32602  0.0000
time                     0.000608 0.00139945 29    0.43425  0.6673
To                      -0.020070 0.04804644 29   -0.41771  0.6792
harmonic(month, 2, 12)1 -0.046565 0.01348757 29   -3.45245  0.0017
harmonic(month, 2, 12)2 -0.011125 0.01383080 29   -0.80437  0.4277
harmonic(month, 2, 12)3 -0.008375 0.01551310 29   -0.53984  0.5934
harmonic(month, 2, 12)4  0.027456 0.01396944 29    1.96546  0.0590
To:I(time - tpand)       0.005289 0.00751703 29    0.70366  0.4873
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.881                                            
To                       0.333 -0.524                                     
harmonic(month, 2, 12)1 -0.136  0.116 -0.108                              
harmonic(month, 2, 12)2  0.004 -0.018  0.129 -0.077                       
harmonic(month, 2, 12)3  0.154 -0.232  0.518 -0.086     0.047             
harmonic(month, 2, 12)4  0.036 -0.009  0.011 -0.099    -0.005     0.016   
To:I(time - tpand)       0.047 -0.022 -0.723  0.174    -0.111    -0.506   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.089   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-3.7262373 -0.3581455  0.1761093  0.6025694  1.2843002 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Dementia Level change 0.98 (0.9 to 1.07), p = 0.679
Norway Dementia Trend change 1.01 (0.99 to 1.02), p = 0.487
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.117:  log likelihood = -53.07
AIC=108.15   AICc=108.27   BIC=109.73
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.512150e-07 (Intr)
time        2.463220e-09 0     
Residual    1.959461e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6       Phi7 
 0.1506030 -0.1937753 -0.2114405  0.2189966 -0.2790947 -0.2905489 -0.2027746 
      Phi8 
-0.3390503 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -8.769438 0.03245071 28 -270.23872  0.0000
time                     0.006112 0.00367790 28    1.66179  0.1077
To                       0.520015 0.03853794 28   13.49357  0.0000
harmonic(month, 2, 12)1 -0.056359 0.03420331 28   -1.64775  0.1106
harmonic(month, 2, 12)2  0.039345 0.01099328 28    3.57904  0.0013
harmonic(month, 2, 12)3 -0.013766 0.03219115 28   -0.42763  0.6722
harmonic(month, 2, 12)4  0.008658 0.01075788 28    0.80479  0.4277
To:I(time - tpand)       0.016125 0.00348782 28    4.62309  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.961                                            
To                       0.740 -0.862                                     
harmonic(month, 2, 12)1 -0.160  0.200 -0.215                              
harmonic(month, 2, 12)2 -0.033  0.007 -0.022  0.053                       
harmonic(month, 2, 12)3 -0.009 -0.025  0.030  0.006     0.198             
harmonic(month, 2, 12)4  0.080 -0.072  0.053 -0.178     0.017     0.000   
To:I(time - tpand)       0.910 -0.925  0.637 -0.126     0.042     0.013   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.056   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.7400470 -0.7185540 -0.2856532  0.8429810  2.3533158 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Dementia Level change 1.68 (1.57 to 1.8), p < 0.001
Peru Dementia Trend change 1.02 (1.01 to 1.02), p < 0.001
Norway Dementia Level change 0.98 (0.9 to 1.07), p = 0.679
Norway Dementia Trend change 1.01 (0.99 to 1.02), p = 0.487
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.035:  log likelihood = -68.95
AIC=139.89   AICc=139.98   BIC=141.76
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.108928e-06 (Intr)
time        3.238175e-09 0     
Residual    1.235787e+00       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.167412767  0.022542593  0.477223450  0.542506875  0.399836676 -0.109792686 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.381923163 -0.364017679 -0.001084507 -0.088470590  0.001789914 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -7.731715 0.09860260 40 -78.41289  0.0000
time                     0.008864 0.00569059 40   1.55766  0.1272
To                       0.302950 0.08798190 40   3.44332  0.0014
harmonic(month, 2, 12)1 -0.050538 0.01433599 40  -3.52526  0.0011
harmonic(month, 2, 12)2 -0.003959 0.01239048 40  -0.31955  0.7510
harmonic(month, 2, 12)3  0.058107 0.01656105 40   3.50867  0.0011
harmonic(month, 2, 12)4  0.021055 0.01225698 40   1.71779  0.0936
To:I(time - tpand)      -0.005785 0.01205794 40  -0.47978  0.6340
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.913                                            
To                       0.117 -0.282                                     
harmonic(month, 2, 12)1 -0.017 -0.003  0.157                              
harmonic(month, 2, 12)2  0.018 -0.073  0.235  0.117                       
harmonic(month, 2, 12)3 -0.118  0.040  0.518  0.045     0.165             
harmonic(month, 2, 12)4  0.036 -0.018 -0.096 -0.080    -0.023    -0.014   
To:I(time - tpand)       0.687 -0.789 -0.216 -0.037    -0.011    -0.341   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)       0.064   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.12026181 -0.75666434 -0.06252793  0.77418090  2.23841353 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Singapore Dementia Level change 1.35 (1.15 to 1.59), p = 0.001
Singapore Dementia Trend change 0.99 (0.97 to 1.02), p = 0.634
Peru Dementia Level change 1.68 (1.57 to 1.8), p < 0.001
Peru Dementia Trend change 1.02 (1.01 to 1.02), p < 0.001
Norway Dementia Level change 0.98 (0.9 to 1.07), p = 0.679
Norway Dementia Trend change 1.01 (0.99 to 1.02), p = 0.487
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot7

plot7

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.039:  log likelihood = -69.03
AIC=140.06   AICc=140.15   BIC=141.93
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.076495e-07 (Intr)
time        1.323334e-09 0     
Residual    1.174546e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.035831545 -0.003135299  0.422780282 -0.011122587 -0.034504578 -0.442731003 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.667525 0.07340919 40 -90.82684  0.0000
time                    -0.001755 0.00456692 40  -0.38418  0.7029
To                       0.044734 0.11152161 40   0.40112  0.6905
harmonic(month, 2, 12)1 -0.009187 0.05182538 40  -0.17727  0.8602
harmonic(month, 2, 12)2 -0.064005 0.02034553 40  -3.14591  0.0031
harmonic(month, 2, 12)3  0.019236 0.05206829 40   0.36944  0.7137
harmonic(month, 2, 12)4  0.027939 0.01995965 40   1.39977  0.1693
To:I(time - tpand)       0.006232 0.00806311 40   0.77286  0.4441
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.896                                            
To                       0.384 -0.605                                     
harmonic(month, 2, 12)1 -0.120  0.085 -0.016                              
harmonic(month, 2, 12)2  0.046 -0.062  0.146 -0.037                       
harmonic(month, 2, 12)3 -0.027 -0.034  0.188  0.045    -0.023             
harmonic(month, 2, 12)4  0.008  0.036 -0.072 -0.067    -0.001    -0.017   
To:I(time - tpand)       0.497 -0.525 -0.239 -0.015    -0.016    -0.189   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.012   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.05468068 -0.50819065 -0.03756207  0.47026019  2.40478815 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Dementia Level change 1.05 (0.85 to 1.28), p = 0.69
Sweden Dementia Trend change 1.01 (0.99 to 1.02), p = 0.444
Singapore Dementia Level change 1.35 (1.15 to 1.59), p = 0.001
Singapore Dementia Trend change 0.99 (0.97 to 1.02), p = 0.634
Peru Dementia Level change 1.68 (1.57 to 1.8), p < 0.001
Peru Dementia Trend change 1.02 (1.01 to 1.02), p < 0.001
Norway Dementia Level change 0.98 (0.9 to 1.07), p = 0.679
Norway Dementia Trend change 1.01 (0.99 to 1.02), p = 0.487
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Dementia
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.045:  log likelihood = -69.17
AIC=140.35   AICc=140.44   BIC=142.22
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.076156e-06 (Intr)
time        7.139642e-09 0     
Residual    1.121613e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.1916488 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -5.308514 0.05781445 40 -91.81986  0.0000
time                     0.005736 0.00359864 40   1.59404  0.1188
To                       0.121377 0.08391107 40   1.44649  0.1558
harmonic(month, 2, 12)1  0.027863 0.02892429 40   0.96331  0.3412
harmonic(month, 2, 12)2 -0.005173 0.02587891 40  -0.19988  0.8426
harmonic(month, 2, 12)3  0.005339 0.02903406 40   0.18389  0.8550
harmonic(month, 2, 12)4  0.005393 0.02585869 40   0.20855  0.8359
To:I(time - tpand)       0.002370 0.00615279 40   0.38519  0.7021
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.867                                            
To                       0.352 -0.602                                     
harmonic(month, 2, 12)1 -0.127  0.087 -0.041                              
harmonic(month, 2, 12)2 -0.034 -0.002  0.047 -0.002                       
harmonic(month, 2, 12)3  0.069 -0.121  0.252 -0.024     0.003             
harmonic(month, 2, 12)4  0.001  0.019 -0.030 -0.028    -0.025    -0.020   
To:I(time - tpand)       0.496 -0.577 -0.162  0.035     0.007    -0.115   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.030   

Standardized Within-Group Residuals:
         Min           Q1          Med           Q3          Max 
-2.709386442 -0.533177495 -0.006147322  0.595662627  1.991083512 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Dementia Level change 1.13 (0.97 to 1.32), p = 0.156
USA Dementia Trend change 1 (0.99 to 1.01), p = 0.702
Sweden Dementia Level change 1.05 (0.85 to 1.28), p = 0.69
Sweden Dementia Trend change 1.01 (0.99 to 1.02), p = 0.444
Singapore Dementia Level change 1.35 (1.15 to 1.59), p = 0.001
Singapore Dementia Trend change 0.99 (0.97 to 1.02), p = 0.634
Peru Dementia Level change 1.68 (1.57 to 1.8), p < 0.001
Peru Dementia Trend change 1.02 (1.01 to 1.02), p < 0.001
Norway Dementia Level change 0.98 (0.9 to 1.07), p = 0.679
Norway Dementia Trend change 1.01 (0.99 to 1.02), p = 0.487
China Dementia Level change 21.92 (5.45 to 88.2), p < 0.001
China Dementia Trend change 1.04 (0.96 to 1.12), p = 0.407
Canada Dementia Level change 1.46 (1.42 to 1.49), p < 0.001
Canada Dementia Trend change 1 (1 to 1), p = 0.904
Australia Dementia Level change 1.9 (1.79 to 2.01), p < 0.001
Australia Dementia Trend change 0.98 (0.98 to 0.99), p < 0.001
Argentina Dementia Level change 1.19 (0.77 to 1.84), p = 0.47
Argentina Dementia Trend change 0.98 (0.94 to 1.01), p = 0.238
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.19 (0.77 to 1.84), p = 0.47

Trend change

0.98 (0.94 to 1.01), p = 0.238

Australia

Level change

1.9 (1.79 to 2.01), p < 0.001

Trend change

0.98 (0.98 to 0.99), p < 0.001

Canada

Level change

1.46 (1.42 to 1.49), p < 0.001

Trend change

1 (1 to 1), p = 0.904

China

Level change

21.92 (5.45 to 88.2), p < 0.001

Trend change

1.04 (0.96 to 1.12), p = 0.407

Norway

Level change

0.98 (0.9 to 1.07), p = 0.679

Trend change

1.01 (0.99 to 1.02), p = 0.487

Peru

Level change

1.68 (1.57 to 1.8), p < 0.001

Trend change

1.02 (1.01 to 1.02), p < 0.001

Singapore

Level change

1.35 (1.15 to 1.59), p = 0.001

Trend change

0.99 (0.97 to 1.02), p = 0.634

Sweden

Level change

1.05 (0.85 to 1.28), p = 0.69

Trend change

1.01 (0.99 to 1.02), p = 0.444

USA

Level change

1.13 (0.97 to 1.32), p = 0.156

Trend change

1 (0.99 to 1.01), p = 0.702

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Outcome: Sleep Disorders

  1. Argentina
  2. Australia
  3. Canada
  4. China
  5. Norway
  6. Peru
  7. Singapore
  8. Sweden
  9. USA
Code
# Set the counter
i <- i + 1

if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Argentina
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 5, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.028:  log likelihood = -68.78
AIC=139.56   AICc=139.65   BIC=141.43
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.267062e-06 (Intr)
time        4.275108e-09 0     
Residual    1.330556e+00       

Correlation Structure: ARMA(5,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5 
 0.11311050  0.24984006  0.41710473 -0.06236869 -0.41430420 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -6.984804 0.07459115 40 -93.64119  0.0000
time                     0.010010 0.00435005 40   2.30101  0.0267
To                       0.424096 0.08775900 40   4.83250  0.0000
harmonic(month, 2, 12)1  0.018199 0.03885242 40   0.46841  0.6420
harmonic(month, 2, 12)2  0.120825 0.01347247 40   8.96832  0.0000
harmonic(month, 2, 12)3 -0.014538 0.03865408 40  -0.37611  0.7088
harmonic(month, 2, 12)4  0.042731 0.01325859 40   3.22288  0.0025
To:I(time - tpand)      -0.034375 0.00785735 40  -4.37492  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.901                                            
To                       0.337 -0.546                                     
harmonic(month, 2, 12)1 -0.050  0.027  0.018                              
harmonic(month, 2, 12)2  0.017 -0.100  0.240  0.143                       
harmonic(month, 2, 12)3  0.010 -0.026  0.196 -0.045     0.038             
harmonic(month, 2, 12)4 -0.037  0.069 -0.102 -0.112    -0.064     0.109   
To:I(time - tpand)       0.560 -0.632 -0.147  0.009    -0.035    -0.155   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.1038711 -0.7774610 -0.2766568  0.7902937  2.1260996 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0

tab |> 
  knitr::kable()
country mh_category effect estimate
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot1

plot1

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Australia
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.065:  log likelihood = -69.62
AIC=141.24   AICc=141.32   BIC=143.11
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 4.324197e-07 (Intr)
time        4.140496e-09 0     
Residual    1.945804e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
 0.04811765 -0.22137991  0.07746226 -0.04553207 -0.24503095 -0.33210074 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -5.492815 0.02323731 40 -236.37918  0.0000
time                     0.003787 0.00142666 40    2.65457  0.0113
To                       0.145081 0.03276823 40    4.42750  0.0001
harmonic(month, 2, 12)1 -0.029261 0.02444995 40   -1.19678  0.2384
harmonic(month, 2, 12)2  0.069948 0.01120997 40    6.23984  0.0000
harmonic(month, 2, 12)3  0.028167 0.02543119 40    1.10759  0.2747
harmonic(month, 2, 12)4 -0.040094 0.01126635 40   -3.55873  0.0010
To:I(time - tpand)      -0.011231 0.00219608 40   -5.11397  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.913                                            
To                       0.489 -0.698                                     
harmonic(month, 2, 12)1 -0.082  0.085 -0.099                              
harmonic(month, 2, 12)2 -0.021 -0.013  0.066  0.008                       
harmonic(month, 2, 12)3  0.126 -0.114  0.128 -0.011     0.045             
harmonic(month, 2, 12)4  0.004  0.022 -0.029 -0.017    -0.001     0.006   
To:I(time - tpand)       0.492 -0.501 -0.152  0.076    -0.012     0.000   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.023   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-2.39546354 -0.74476646 -0.02574487  0.76296003  2.18884296 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot2

plot2

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Canada
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 11, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.048:  log likelihood = -69.22
AIC=140.45   AICc=140.53   BIC=142.32
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.991070e-07 (Intr)
time        3.249718e-09 0     
Residual    4.317011e-01       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.142128550 -0.040349914 -0.002853830 -0.270478260 -0.272741739 -0.305526321 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.002598161 -0.128557375 -0.140535724 -0.401501671 -0.137280831 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -6.303782 0.008248348 40 -764.2478  0.0000
time                    -0.003247 0.000535143 40   -6.0667  0.0000
To                       0.171792 0.014044859 40   12.2316  0.0000
harmonic(month, 2, 12)1 -0.003739 0.010037904 40   -0.3725  0.7115
harmonic(month, 2, 12)2  0.012350 0.006094405 40    2.0264  0.0494
harmonic(month, 2, 12)3 -0.022662 0.009794821 40   -2.3137  0.0259
harmonic(month, 2, 12)4 -0.014487 0.006049641 40   -2.3947  0.0214
To:I(time - tpand)       0.006452 0.000850782 40    7.5840  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.936                                            
To                       0.581 -0.748                                     
harmonic(month, 2, 12)1 -0.197  0.187 -0.147                              
harmonic(month, 2, 12)2 -0.043  0.028  0.004  0.012                       
harmonic(month, 2, 12)3  0.062 -0.077  0.166 -0.028     0.001             
harmonic(month, 2, 12)4 -0.010  0.012  0.018 -0.039    -0.040    -0.007   
To:I(time - tpand)       0.339 -0.294 -0.336  0.037    -0.008    -0.161   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.072   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9025822 -0.8322937 -0.3121056  0.8858935  2.4667662 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot3

plot3

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: China
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_china(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 2, q = 1, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
Error in lme.formula(fixed = zz ~ time + To + harmonic(month, 2, 12) + : nlminb problem, convergence error code = 1
  message = singular convergence (7)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.048:  log likelihood = -69.22
AIC=140.45   AICc=140.53   BIC=142.32
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.991070e-07 (Intr)
time        3.249718e-09 0     
Residual    4.317011e-01       

Correlation Structure: ARMA(11,0)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi1         Phi2         Phi3         Phi4         Phi5         Phi6 
-0.142128550 -0.040349914 -0.002853830 -0.270478260 -0.272741739 -0.305526321 
        Phi7         Phi8         Phi9        Phi10        Phi11 
-0.002598161 -0.128557375 -0.140535724 -0.401501671 -0.137280831 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -6.303782 0.008248348 40 -764.2478  0.0000
time                    -0.003247 0.000535143 40   -6.0667  0.0000
To                       0.171792 0.014044859 40   12.2316  0.0000
harmonic(month, 2, 12)1 -0.003739 0.010037904 40   -0.3725  0.7115
harmonic(month, 2, 12)2  0.012350 0.006094405 40    2.0264  0.0494
harmonic(month, 2, 12)3 -0.022662 0.009794821 40   -2.3137  0.0259
harmonic(month, 2, 12)4 -0.014487 0.006049641 40   -2.3947  0.0214
To:I(time - tpand)       0.006452 0.000850782 40    7.5840  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.936                                            
To                       0.581 -0.748                                     
harmonic(month, 2, 12)1 -0.197  0.187 -0.147                              
harmonic(month, 2, 12)2 -0.043  0.028  0.004  0.012                       
harmonic(month, 2, 12)3  0.062 -0.077  0.166 -0.028     0.001             
harmonic(month, 2, 12)4 -0.010  0.012  0.018 -0.039    -0.040    -0.007   
To:I(time - tpand)       0.339 -0.294 -0.336  0.037    -0.008    -0.161   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.072   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9025822 -0.8322937 -0.3121056  0.8858935  2.4667662 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
China Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
China Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot4

plot4

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Norway
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_norway(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE, 
          mean_cond = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,4) with zero mean 

Coefficients:
          ma1      ma2     ma3     ma4
      -0.3056  -0.7032  0.5177  0.3206
s.e.   0.1613   0.1470  0.1887  0.1597

sigma^2 = 0.7682:  log likelihood = -46.9
AIC=103.79   AICc=105.73   BIC=111.85
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.864192e-07 (Intr)
time        9.585366e-09 0     
Residual    3.790751e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
        Phi 
-0.09330685 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -4.881016 0.01601878 29 -304.70585  0.0000
time                     0.005163 0.00097888 29    5.27422  0.0000
To                       0.000447 0.03354400 29    0.01334  0.9895
harmonic(month, 2, 12)1  0.003402 0.00962943 29    0.35333  0.7264
harmonic(month, 2, 12)2  0.024326 0.00950774 29    2.55860  0.0160
harmonic(month, 2, 12)3  0.104350 0.01104340 29    9.44905  0.0000
harmonic(month, 2, 12)4  0.042665 0.00959841 29    4.44501  0.0001
To:I(time - tpand)      -0.000689 0.00512560 29   -0.13438  0.8940
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.875                                            
To                       0.277 -0.490                                     
harmonic(month, 2, 12)1 -0.129  0.095 -0.076                              
harmonic(month, 2, 12)2 -0.015 -0.011  0.146 -0.138                       
harmonic(month, 2, 12)3  0.087 -0.204  0.515 -0.079     0.038             
harmonic(month, 2, 12)4  0.018  0.011 -0.018 -0.077    -0.012    -0.049   
To:I(time - tpand)       0.102 -0.061 -0.723  0.144    -0.137    -0.508   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.070   

Standardized Within-Group Residuals:
         Min           Q1          Med           Q3          Max 
-3.289767980 -0.613120995 -0.003955552  0.620726953  2.474708164 

Number of Observations: 37
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Norway Sleep Disorders Level change 1 (0.94 to 1.06), p = 0.989
Norway Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.894
China Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
China Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot5

plot5

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Peru
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 8, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.059:  log likelihood = -52.11
AIC=106.23   AICc=106.35   BIC=107.81
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 7.430648e-08 (Intr)
time        1.127635e-09 0     
Residual    2.778069e+00       

Correlation Structure: ARMA(8,0)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi1       Phi2       Phi3       Phi4       Phi5       Phi6       Phi7 
-0.1849571 -0.4290715 -0.5244917 -0.4095390 -0.6113681 -0.3807415 -0.3283361 
      Phi8 
-0.1298395 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value   Std.Error DF   t-value p-value
(Intercept)             -8.390605 0.016474965 28 -509.2943  0.0000
time                     0.008756 0.001789061 28    4.8941  0.0000
To                       0.705928 0.017345641 28   40.6977  0.0000
harmonic(month, 2, 12)1 -0.025546 0.009781032 28   -2.6118  0.0143
harmonic(month, 2, 12)2  0.046000 0.014199017 28    3.2397  0.0031
harmonic(month, 2, 12)3 -0.042392 0.011091641 28   -3.8220  0.0007
harmonic(month, 2, 12)4 -0.017401 0.013788134 28   -1.2620  0.2173
To:I(time - tpand)      -0.027843 0.001831428 28  -15.2027  0.0000
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.963                                            
To                       0.716 -0.835                                     
harmonic(month, 2, 12)1  0.070 -0.029 -0.013                              
harmonic(month, 2, 12)2 -0.040 -0.001 -0.008 -0.191                       
harmonic(month, 2, 12)3  0.413 -0.413  0.247 -0.022     0.085             
harmonic(month, 2, 12)4  0.015 -0.006  0.020  0.021    -0.019    -0.108   
To:I(time - tpand)       0.902 -0.919  0.592  0.042     0.049     0.479   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.019   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-1.9804777 -0.4890623  0.1311510  0.7252228  2.1713010 

Number of Observations: 36
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Peru Sleep Disorders Level change 2.03 (1.96 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.97 (0.97 to 0.98), p < 0.001
Norway Sleep Disorders Level change 1 (0.94 to 1.06), p = 0.989
Norway Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.894
China Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
China Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot6

plot6

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Singapore
  • Mental Health Category: Sleep Disorders

Modeling Sleep Disorders for Singapore is not appropiate due to missing data in numerator.

Code
fake <- data.frame(x = seq(1, 48, 1), 
                   y = seq(1, 48, 1))

fake |> 
  ggplot(aes(x = x, y = y)) + 
  theme_void() + 
  annotate(geom = "text", 
           x = 24, 
           y = 24, 
           label = "Not calculable", 
           color = "black", 
           size = 6) -> plot7
Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: Sweden
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 6, q = 0, 
          data = mh_visits5b)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(0,0,0) with zero mean 

sigma^2 = 1.045:  log likelihood = -69.16
AIC=140.32   AICc=140.41   BIC=142.19
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 5.212305e-07 (Intr)
time        2.308023e-09 0     
Residual    1.028689e+00       

Correlation Structure: ARMA(6,0)
 Formula: ~time | country 
 Parameter estimate(s):
       Phi1        Phi2        Phi3        Phi4        Phi5        Phi6 
-0.03322689  0.12777293 -0.09228121 -0.20220267 -0.38080556  0.29025035 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF    t-value p-value
(Intercept)             -6.195730 0.04201351 40 -147.46996  0.0000
time                    -0.021831 0.00274040 40   -7.96648  0.0000
To                       0.461240 0.07157317 40    6.44431  0.0000
harmonic(month, 2, 12)1  0.108932 0.03162640 40    3.44434  0.0014
harmonic(month, 2, 12)2  0.045489 0.02962554 40    1.53546  0.1325
harmonic(month, 2, 12)3  0.182784 0.03359565 40    5.44070  0.0000
harmonic(month, 2, 12)4  0.005516 0.02907081 40    0.18976  0.8505
To:I(time - tpand)       0.001982 0.00516082 40    0.38406  0.7030
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.869                                            
To                       0.386 -0.627                                     
harmonic(month, 2, 12)1 -0.190  0.128 -0.095                              
harmonic(month, 2, 12)2 -0.064  0.052  0.020 -0.182                       
harmonic(month, 2, 12)3  0.083 -0.171  0.216  0.011    -0.128             
harmonic(month, 2, 12)4 -0.031  0.064 -0.049  0.051    -0.016    -0.168   
To:I(time - tpand)       0.403 -0.449 -0.278  0.048    -0.043    -0.039   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.053   

Standardized Within-Group Residuals:
        Min          Q1         Med          Q3         Max 
-1.82690950 -0.74078576 -0.04606689  0.73865294  2.58677655 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
Sweden Sleep Disorders Level change 1.59 (1.39 to 1.81), p < 0.001
Sweden Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.703
Peru Sleep Disorders Level change 2.03 (1.96 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.97 (0.97 to 0.98), p < 0.001
Norway Sleep Disorders Level change 1 (0.94 to 1.06), p = 0.989
Norway Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.894
China Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
China Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot8

plot8

Code
# Set the counter
i <- i + 1
if (i %% 9 == 0) {
    j <- (i - 1) %/% 9 + 1
  } else {
    j <- i %/% 9 + 1
  }
  • Country: USA
  • Mental Health Category: Sleep Disorders
Code
# Model
mod_full <- sti_model_usa(country = countryc[i], 
          mh_category = mh_categoryc[j], 
          p = 1, q = 0, 
          data = mh_visits5b, 
          shock_exclude = TRUE)
  • The autocorrelation function of residuals from a naive glm that does not account for autocorrelation is the following:
Code
mod_full$auto_plot; mod_full$arima

Series: rst 
ARIMA(1,0,0) with zero mean 

Coefficients:
         ar1
      0.3112
s.e.  0.1359

sigma^2 = 0.9593:  log likelihood = -66.66
AIC=137.31   AICc=137.58   BIC=141.06
  • The normalized residuasl from a GLMMM that take into account dependence of residuls around the time shows no autocorrelation:
Code
mod_full$dx.plot

The final model is the following:

Code
summary(mod_full$mod)
Linear mixed-effects model fit by maximum likelihood
  Data: mh_visitsm 
  AIC BIC logLik
   NA  NA     NA

Random effects:
 Formula: ~time | country
 Structure: General positive-definite, Log-Cholesky parametrization
            StdDev       Corr  
(Intercept) 1.454966e-06 (Intr)
time        8.232366e-09 0     
Residual    1.147776e+00       

Correlation Structure: AR(1)
 Formula: ~time | country 
 Parameter estimate(s):
      Phi 
0.3330002 
Variance function:
 Structure: fixed weights
 Formula: ~invwt 
Fixed effects:  numerator ~ 1 + time + To + I(time - tpand):To + harmonic(month,      2, 12) + offset(log(denominator)) 
                            Value  Std.Error DF   t-value p-value
(Intercept)             -4.424697 0.04645035 40 -95.25649  0.0000
time                    -0.013510 0.00306047 40  -4.41443  0.0001
To                       0.172465 0.07512029 40   2.29586  0.0270
harmonic(month, 2, 12)1  0.013427 0.02502992 40   0.53642  0.5946
harmonic(month, 2, 12)2 -0.040008 0.02031986 40  -1.96889  0.0559
harmonic(month, 2, 12)3  0.035110 0.02488268 40   1.41103  0.1660
harmonic(month, 2, 12)4 -0.003025 0.02033303 40  -0.14878  0.8825
To:I(time - tpand)       0.023900 0.00563334 40   4.24264  0.0001
 Correlation: 
                        (Intr) time   To     h(,2,12)1 h(,2,12)2 h(,2,12)3
time                    -0.845                                            
To                       0.321 -0.578                                     
harmonic(month, 2, 12)1 -0.166  0.124 -0.052                              
harmonic(month, 2, 12)2 -0.051  0.023  0.038 -0.011                       
harmonic(month, 2, 12)3  0.024 -0.079  0.221 -0.017    -0.023             
harmonic(month, 2, 12)4 -0.003  0.027 -0.032 -0.026    -0.035    -0.035   
To:I(time - tpand)       0.470 -0.570 -0.199  0.004    -0.013    -0.141   
                        h(,2,12)4
time                             
To                               
harmonic(month, 2, 12)1          
harmonic(month, 2, 12)2          
harmonic(month, 2, 12)3          
harmonic(month, 2, 12)4          
To:I(time - tpand)      -0.037   

Standardized Within-Group Residuals:
       Min         Q1        Med         Q3        Max 
-2.6452082 -0.6778055 -0.1322661  0.6368199  2.0743336 

Number of Observations: 48
Number of Groups: 1 
Code
parameters::model_parameters(mod_full$mod) |> 
  dplyr::select(Parameter, Coefficient, CI_low, CI_high, p) |> 
  filter(Parameter %in% c("To", "To:I(time - tpand)")) |> 
  as_data_frame() |> 
  rename(est = Coefficient, 
         ll = CI_low, 
         ul = CI_high) |> 
  mutate(effect = c("Level change", "Trend change"), 
         across(c(est, ll, ul), ~(round(exp(.x) , 2))), 
         p = if_else(p < 0.001, "p < 0.001", 
                     as.character(paste0("p = ", round(p, 3)))), 
         estimate = str_glue("{est} ({ll} to {ul}), {p}"), 
         estimate = as.character(estimate), 
         mh_category = mh_categoryc[j], 
         country = countryc[i]
         ) |> 
  dplyr::select(country, mh_category, effect, estimate) -> tab0

tab <- tab0 |>
  bind_rows(tab)

tab |> 
  knitr::kable()
country mh_category effect estimate
USA Sleep Disorders Level change 1.19 (1.03 to 1.36), p = 0.027
USA Sleep Disorders Trend change 1.02 (1.01 to 1.03), p < 0.001
Sweden Sleep Disorders Level change 1.59 (1.39 to 1.81), p < 0.001
Sweden Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.703
Peru Sleep Disorders Level change 2.03 (1.96 to 2.09), p < 0.001
Peru Sleep Disorders Trend change 0.97 (0.97 to 0.98), p < 0.001
Norway Sleep Disorders Level change 1 (0.94 to 1.06), p = 0.989
Norway Sleep Disorders Trend change 1 (0.99 to 1.01), p = 0.894
China Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
China Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Canada Sleep Disorders Level change 1.19 (1.16 to 1.22), p < 0.001
Canada Sleep Disorders Trend change 1.01 (1 to 1.01), p < 0.001
Australia Sleep Disorders Level change 1.16 (1.09 to 1.23), p < 0.001
Australia Sleep Disorders Trend change 0.99 (0.98 to 0.99), p < 0.001
Argentina Sleep Disorders Level change 1.53 (1.3 to 1.8), p < 0.001
Argentina Sleep Disorders Trend change 0.97 (0.95 to 0.98), p < 0.001
Code
sti_effect_ploter(mod_full, 
                  alfa = alfa0,
                  alfa_ribbon = alfa_ribbon0,
                  color_point = color_point0, 
                  color_line = color_line0, 
                  color_ribbon_ex = color_ribbon_ex0,
                  color_ribbon_def = color_ribbon_def0, 
                  color_interrupt = color_interrupt0,
                  mindate = mindate0, 
                  maxdate = maxdate0) -> plot9

plot9

Table

Code
# Cargar la biblioteca

tab_f <- tab |> 
  arrange(country, effect) |> 
  mutate(
    country_effect = if_else(effect == "Level change", country, NA_character_),
    effect = case_when(
      effect == "Level change" ~ "   Level change",
      effect == "Trend change" ~ "   Trend change"
    )
  ) |> 
  dplyr::select(-mh_category, -country) |> 
  dplyr::select(country_effect, effect, estimate)

# Crear la tabla flextable
ft <- flextable(tab_f)

# Cambiar los nombres de las columnas
ft <- set_header_labels(ft, 
                        country_effect = "Country",
                        effect = "Effect measure", 
                        estimate = "IRR (95%CI), p-value")

# Formatear la tabla
ft <- ft %>%
  align(align = "left") %>%
  padding(padding = 5) %>%
  fontsize(size = 11) %>%
  bold(part = "header") %>%
  bold(i = which(!is.na(tab_f$country_effect)), j = "country_effect") %>%
  autofit()

# Guardar como documento de Word
save_as_docx(ft, 
             path = here("table_effects", 
                         paste0("table_", mh_categoryc[j], ".docx")))

ft

Country

Effect measure

IRR (95%CI), p-value

Argentina

Level change

1.53 (1.3 to 1.8), p < 0.001

Trend change

0.97 (0.95 to 0.98), p < 0.001

Australia

Level change

1.16 (1.09 to 1.23), p < 0.001

Trend change

0.99 (0.98 to 0.99), p < 0.001

Canada

Level change

1.19 (1.16 to 1.22), p < 0.001

Trend change

1.01 (1 to 1.01), p < 0.001

China

Level change

1.19 (1.16 to 1.22), p < 0.001

Trend change

1.01 (1 to 1.01), p < 0.001

Norway

Level change

1 (0.94 to 1.06), p = 0.989

Trend change

1 (0.99 to 1.01), p = 0.894

Peru

Level change

2.03 (1.96 to 2.09), p < 0.001

Trend change

0.97 (0.97 to 0.98), p < 0.001

Sweden

Level change

1.59 (1.39 to 1.81), p < 0.001

Trend change

1 (0.99 to 1.01), p = 0.703

USA

Level change

1.19 (1.03 to 1.36), p = 0.027

Trend change

1.02 (1.01 to 1.03), p < 0.001

Plot

Code
((plot1 | plot2 | plot3) / 
  (plot4 | plot5 | plot6) / 
  (plot7 | plot8 | plot9)) + 
  plot_layout(guides = 'collect') + 
  plot_annotation(tag_levels = 'A') & 
  theme(legend.position="bottom") -> plot_effect_final

ggsave(filename = paste0(mh_categoryc[j], ".png"), 
       plot = plot_effect_final, 
       device = "png", 
       path = here("img", "plot_effects"), 
       scale = 1.5, 
       width = 18, 
       height = 18, 
       units = "cm", 
       dpi = 900)
Code
knitr::include_graphics(here("img", 
                             "plot_effects", 
                             paste0(mh_categoryc[j], ".png")))

Reproducibility Ticket

Code
sessionInfo()
R version 4.3.0 (2023-04-21 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 11 x64 (build 22621)

Matrix products: default


locale:
[1] LC_COLLATE=Spanish_Peru.utf8  LC_CTYPE=Spanish_Peru.utf8   
[3] LC_MONETARY=Spanish_Peru.utf8 LC_NUMERIC=C                 
[5] LC_TIME=Spanish_Peru.utf8    

time zone: America/Lima
tzcode source: internal

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

other attached packages:
 [1] imputeTS_3.3        flextable_0.9.1     parameters_0.21.0  
 [4] qqplotr_0.0.6       feasts_0.3.1        fabletools_0.3.3   
 [7] tsibble_1.1.3       DHARMa_0.4.6        zoo_1.8-12         
[10] tsModel_0.6-1       mgcv_1.8-42         MASS_7.3-59        
[13] forecast_8.21       nlme_3.1-162        lme4_1.1-33        
[16] Matrix_1.5-4        ggeffects_1.2.1     broom.mixed_0.2.9.4
[19] broom_1.0.4         gtsummary_1.7.1     janitor_2.2.0      
[22] DT_0.27             scales_1.2.1        patchwork_1.1.2    
[25] lubridate_1.9.2     forcats_1.0.0       stringr_1.5.0      
[28] dplyr_1.1.2         purrr_1.0.1         readr_2.1.4        
[31] tidyr_1.3.0         tibble_3.2.1        ggplot2_3.4.2      
[34] tidyverse_2.0.0     here_1.0.1          rio_0.5.29         
[37] pacman_0.5.1       

loaded via a namespace (and not attached):
  [1] splines_4.3.0           later_1.3.1             bitops_1.0-7           
  [4] cellranger_1.1.0        datawizard_0.7.1        xts_0.13.1             
  [7] lifecycle_1.0.3         doParallel_1.0.17       rprojroot_2.0.3        
 [10] globals_0.16.2          lattice_0.21-8          crosstalk_1.2.0        
 [13] insight_0.19.1          backports_1.4.1         magrittr_2.0.3         
 [16] sass_0.4.6              openxlsx_4.2.5.2        rmarkdown_2.22         
 [19] jquerylib_0.1.4         yaml_2.3.7              fracdiff_1.5-2         
 [22] qqconf_1.3.2            httpuv_1.6.9            zip_2.3.0              
 [25] askpass_1.1             minqa_1.2.5             multcomp_1.4-23        
 [28] quadprog_1.5-8          nnet_7.3-18             pracma_2.4.2           
 [31] TH.data_1.1-2           sandwich_3.0-2          gdtools_0.3.3          
 [34] listenv_0.9.0           crul_1.3                anytime_0.3.9          
 [37] parallelly_1.35.0       codetools_0.2-19        ggtext_0.1.2           
 [40] xml2_1.3.4              tidyselect_1.2.0        httpcode_0.3.0         
 [43] memuse_4.2-3            farver_2.1.1            urca_1.3-3             
 [46] broom.helpers_1.13.0    jsonlite_1.8.4          opdisDownsampling_0.8.2
 [49] ellipsis_0.3.2          survival_3.5-5          iterators_1.0.14       
 [52] emmeans_1.8.6           systemfonts_1.0.4       foreach_1.5.2          
 [55] tools_4.3.0             twosamples_2.0.0        ragg_1.2.5             
 [58] Rcpp_1.0.10             glue_1.6.2              xfun_0.39              
 [61] TTR_0.24.3              distributional_0.3.2    withr_2.5.0            
 [64] fastmap_1.1.1           boot_1.3-28.1           fansi_1.0.4            
 [67] openssl_2.0.6           caTools_1.18.2          digest_0.6.31          
 [70] timechange_0.2.0        R6_2.5.1                mime_0.12              
 [73] estimability_1.4.1      textshaping_0.3.6       colorspace_2.1-0       
 [76] utf8_1.2.3              generics_0.1.3          fontLiberation_0.1.0   
 [79] data.table_1.14.8       robustbase_0.95-1       httr_1.4.6             
 [82] htmlwidgets_1.6.2       pkgconfig_2.0.3         gtable_0.3.3           
 [85] timeDate_4022.108       lmtest_0.9-40           furrr_0.3.1            
 [88] htmltools_0.5.5         fontBitstreamVera_0.1.1 stinepack_1.4          
 [91] tseries_0.10-54         png_0.1-8               snakecase_0.11.0       
 [94] knitr_1.42              rstudioapi_0.14         tzdb_0.3.0             
 [97] uuid_1.1-0              coda_0.19-4             curl_5.0.0             
[100] nloptr_2.0.3            cachem_1.0.8            parallel_4.3.0         
[103] foreign_0.8-84          pillar_1.9.0            grid_4.3.0             
[106] vctrs_0.6.2             promises_1.2.0.1        xtable_1.8-4           
[109] evaluate_0.21           mvtnorm_1.1-3           cli_3.6.1              
[112] compiler_4.3.0          rlang_1.1.1             crayon_1.5.2           
[115] labeling_0.4.2          stringi_1.7.12          munsell_0.5.0          
[118] bayestestR_0.13.1       fontquiver_0.2.1        benchmarkme_1.0.8      
[121] hms_1.1.3               future_1.32.0           gfonts_0.2.0           
[124] shiny_1.7.4             haven_2.5.2             gridtext_0.1.5         
[127] gt_0.9.0                bslib_0.4.2             quantmod_0.4.22        
[130] benchmarkmeData_1.0.4   DEoptimR_1.0-13         readxl_1.4.2           
[133] officer_0.6.2